代码如下
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
}circle;
circle *Create(circle *head,int n)
{
int i;
head=(circle*)malloc(sizeof(circle));
head->num =1;
head->next=head;
circle *tail,*p;
tail=head;
for(i=2;i<=n;i++)
{
p=(circle*)malloc(sizeof(circle));
p->num =i;
p->next =tail->next ;
tail->next =p;
tail=p;
}
p->next =head;
return head;//必须要return head
}
int main()
{
int n,k,m,data,i;
int sum;
sum=0;
circle *head,*p,*q,*temp;
scanf("%d,%d,%d",&n,&k,&m);
data=n;
if(k>n)
printf("k should not bigger than n.\n");
else if(n<1||k<1||m<1)
{
printf("n,m,k must bigger than 0.\n");
}
else
{
head=Create(head,n);
p=head;
for(i=2;i<=k;i++)
{
p=p->next ;
}
q=p;//q表示要出列的人
while(data>=1)
{
//当链表里的元素大于等于1
for(i=2;i<m;i++)
{
q=q->next ;
}
temp=q->next ;
q->next =temp->next ;
if(data>1)
{
//判断一下一行有没有十个
if(sum==9)
{
printf("%d\n",temp->num);
sum=0;
}
else if(sum<9)
{
printf("%d ",temp->num );sum++;
}
}
else if(data==1)
printf("%d\n",temp->num );
free(temp);
q=q->next ;
data--;
}
}
}
AC