-
Notifications
You must be signed in to change notification settings - Fork 0
/
danlian4.c
executable file
·83 lines (71 loc) · 1.08 KB
/
danlian4.c
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
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
struct node
{
int number;
struct node *next;
};
typedef struct node V_NODE;
V_NODE *init_circle(int n)
{
int i = 0;
V_NODE *head = NULL;
V_NODE *p = NULL;
head = p = malloc(sizeof(V_NODE));
if(p == NULL)
{
perror("malloc");
exit(0);
}
p->number = 1;
p->next = NULL;
for(i=1; i<n; i++)
{
p->next = malloc(sizeof(V_NODE));
if(p->next == NULL)
{
perror("malloc");
exit(0);
}
p->next->number = i+1;
p->next->next = NULL;
p = p->next;
}
p->next = head;
return p;
}
void j_game(V_NODE *p_f)
{
V_NODE *p = p_f->next;
int interval = 4;
int out_counter = 0;
while(p != p->next)
{
out_counter++;
if(out_counter == interval)
{
out_counter = 0;
printf("%6d(out)\n", p->number);
p_f->next = p->next;
free(p);
p = p_f->next;
}
else
{
printf("%6d", p->number);
p = p->next;
p_f = p_f->next;
}
}
printf("%6d left\n", p->number);
}
int main(void)
{
int n = 0;
V_NODE *rear = NULL;
printf("Please input n:\n");
scanf("%d", &n);
rear = init_circle(n);
j_game(rear);
return 0;
}