输入:5
输出:4
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
node * next;
}node;
int main()
{
int n;
cin>>n;
//建立循环链表
node *head=new node;
head->data=n;
head->next=NULL;
node *tail=head;
for(int i=n-1;i>=1;--i)
{
node *p=new node;
p->data=i;
p->next=head;
head=p;
}
tail->next=head;//首尾相连
//删除
while(head->next!=head)
{
node *q=head->next;
node *p=head->next->next;//要删除的节点
q->next=p->next;
head=p->next;
delete p;
}
cout<<head->data<<endl;
return 0;
}
输入
10
2 5
5
2 3
0
0 0
输出
6,1,7,3,10,9,2,5,8,4
4,2,1,3,5
代码:
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
node * next;
}node;
int main()
{
int n,s,m;
while(cin>>n>>s>>m)
{
if(n+s+m==0) break;
node * head=new node;
head->data=n;
head->next=NULL;
node *tail=head;
for(int i=n-1;i>=1;--i)
{
node *p=new node;
p->data=i;
p->next=head;
head=p;
}
tail->next=head;
s=s-1;
while(s--)
{
head=head->next;
}
if(m==1)
{
while(head!=tail)
{
cout<<head->data<<",";
head=head->next;
}
cout<<head->data<<endl;
}
else
{
while(head->next!=head)
{
node *pre=head,*after;
int t=m-2;
while(t--)//找要删除节点的前一个节点
{
pre=pre->next;
}
after=pre->next;//要删除的节点
pre->next=after->next;
head=after->next;
cout<<after->data<<",";
delete after;
}
cout<<head->data<<endl;
}
}
return 0;
}