解题思路:
注意事项:
参考代码:
#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 人评分
C语言考试练习题_保留字母 (C语言代码)浏览:733 |
钟神赛车 (C语言代码)浏览:911 |
时间转换 (Java代码)浏览:617 |
C语言训练-排序问题<1> (C++代码)浏览:632 |
蛇行矩阵 (C语言代码)浏览:606 |
循环入门练习5 (C语言代码)浏览:907 |
罗列完美数 (C语言代码)浏览:519 |
模拟计算器 (C语言代码)浏览:2366 |
printf基础练习 (C语言代码)浏览:2268 |
幸运数 (C++代码)浏览:2982 |