蔡子经数据结构1.17

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



#include <stdlib.h>

typedef struct
{
int val;
struct * next;
} node;

node* createNode()
{
node *ret = (node*)malloc(sizeof(node));
ret->val = 0;
ret->next = NULL;
return ret;
}

void print(node *head)
{
node *p = head->next;
while(p != head)
{
printf("%d ",p->val);
p = p->next;
}
puts("");
return ;
}

int main()
{
int n,i,k;
scanf("%d %d %d",&n,&i,&k);
node *head = createNode();
head->next = head;
for(int i = n; i >= 1; --i)
{
node *p = createNode();
p->val = i;
p->next = head->next;
head->next = p;
}
print(head);
int cnt = 1;
node *pre = head;
while(cnt != i)
{
++cnt;
pre = pre->next;
}
node *p = pre->next;
cnt = 0;
while(pre != p)
{
while(pre != p)
{
if(p->val != 0) ++cnt;
if(cnt == k) break;
pre = pre->next;
p = p->next;
}
pre->next = p->next;
printf("%d ",p->val);
cnt = 0;
free(p);
p = pre->next;
}
return 0;
}