文章
84
粉丝
408
获赞
33
访问
872.0k
按照题意,这是一个循环链表问题,先建链表,再循环出链表
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
int number;
struct Node* next;
}LNode;
int main()
{
int n;
cin >> n;
LNode *head, *p, *pre = NULL;
p = head = new LNode;
for (int i = 1; i < n; i++) {
p->number = i;
p->next = new LNode;
p = p->next;
}
p->number = n;
p->next = head;//形成循环链表
//开始击鼓传花
p = head;
int count = 1;//计数
while (p->next != p) {
if (count%3 == 0) {
//出链表
pre->next = p->next;
free(p);
p = pre;
}
count++;
pre = p;
p = p->next;
}
cout << p->number << endl;
return 0;
}
登录后发布评论
暂无评论,来抢沙发