-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1110.cpp
More file actions
76 lines (74 loc) · 1.23 KB
/
1110.cpp
File metadata and controls
76 lines (74 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
int siz;
struct no
{
int n;
struct no *prox;
};
typedef struct
{
struct no *inicio;
struct no *fim;
}fila;
void create(fila *q)
{
siz=0;
q->inicio=NULL;
q->fim=NULL;
}
void inserir(fila *q, int v)
{
siz++;
struct no *aux;
aux=(struct no *)malloc(sizeof(struct no));
if(aux==NULL)return ;
aux->n=v;
aux->prox=NULL;
if(q->inicio==NULL)q->inicio=aux;
if(q->fim!=NULL)q->fim->prox=aux;
q->fim=aux;
}
bool Empty(fila q)
{
if(q.inicio==NULL && q.fim==NULL)return true;
return false;
}
void remover(fila *q)
{
siz--;
struct no *aux;
if(q->inicio==NULL)return ;
aux=q->inicio;
q->inicio=aux->prox;
if(q->inicio==NULL)q->fim=NULL;
free(aux);
}
int top(fila q)
{
return q.inicio->n;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
fila q;
create(&q);
for(int i=1;i<=n;i++)
{
inserir(&q,i);
}
printf("Discarded cards: ");
while(siz>1)
{
if(siz!=2)printf("%d, ",top(q));
else printf("%d\n",top(q));
remover(&q);
inserir(&q,top(q));
remover(&q);
}
printf("Remaining card: %d\n",top(q));
}
return 0;
}