代码:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int num;
struct Node* next;
};
//链表实现报到3退圈(13个人)
void q9_6() {
int i, sign = 0, count = 13;
Node *head = (Node *)malloc(sizeof(Node)) ,*pre;
Node *p = (Node *)malloc(sizeof(Node));
p->num = 1;
p->next = NULL;
head->next = p;
for (i = 2; i <= 13; i++) { //构造循环链表
Node *q = (Node *)malloc(sizeof(Node));
q->num = i;
q->next = NULL;
p->next = q;
p = q;
if (i == 13) {
q->next = head->next;
}
}
i = 1;
p = head->next;
pre = head;
while (count >1) {
if (i % 3 == 0) {//喊了3的从链表中删除
pre->next = p->next;
free(p);
p = pre->next;
count--; //每删除一个总数减一,直到剩一个
}
else
{
pre = p;
p = p->next;
}
i++;
}
printf("%d", pre->num);
}
int main() {
q9_6();
system("pause");
return 0;
}
运行: