解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int num;
struct Node *next;
}node;
int size = sizeof(node);
int main(void)
{
node *phead = NULL, *p1, *p2 = NULL, *p3 = NULL;
int j, n, count, count1;
while (scanf("%d", &n) != EOF)
{
for (j = 1;j <= n;j++)
{
p1 = (node*)malloc(size);
p1->num = j;
p1->next = NULL;
if (j == 1)
phead = p1;
else
p2->next = p1;
p2 = p1;
}
//监督单位元
p1 = (node*)malloc(size);
p1->num = -1;
p1->next = NULL;
p2->next = p1;
p2->next->next = phead;
p3 = phead;
while (p3->next != phead)
p3 = p3->next;
p1 = phead;
count = n;
count1 = 0;
while (count != 2)
{
count1++;
if (count1 == 2)
{
count1 = 0;
count--;
printf("%d ", p1->num);
p3->next = p1->next;
p2 = p1;
p1 = p1->next;
if (p1->num == -1)
{
p3 = p1;
p1 = phead;
count1 = 0;
}
free(p2);
continue;
}
p3 = p1;
p1 = p1->next;
if (p1->num == -1)
{
p3 = p1;
p1 = phead;
count1 = 0;
}
}
printf("\n");
p1 = phead;
while (1)
{
if (p1->next->num == -1)
{
printf("%d\n", p1->num);
break;
}
else
printf("%d ", p1->num);
p1 = p1->next;
}
}
return 0;
}
0.0分
2 人评分