用循环双向链表模拟
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct s{
int num;
struct s *qian;
struct s *hou;
int flag;
};
int n,k,m;
int main()
{
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
if(!n&&!k&&!m)
break;
struct s *head,*tail,*p;
p=(struct s*)malloc(sizeof(struct s));
p->num=1;
p->qian=NULL;
p->hou=NULL;
p->flag=1;
head=tail=p;
for(int i=2;i<=n;i++)
{
p=(struct s*)malloc(sizeof(struct s));
p->num=i;
p->qian=tail;
p->hou=NULL;
p->flag=1;
tail->hou=p;
tail=p;
}
tail->hou=head;
head->qian=tail;
/*p=tail;
while(p)
{
printf(" %d",p->num);
p=p->hou;
if(p==tail)
break;
}*/
struct s *q;
p=head;
q=tail;
int i;
while(n>0)
{
for(i=1;i<k;)
{
p=p->hou;
if(p->flag==1)
{
i++;
}
}
for(i=1;i<m;)
{
//else
q=q->qian;
if(q->flag==1)
i++;
}
if(p->num==q->num)
{
printf("%3d",q->num);
p->flag=0;
q->flag=0;
n--;
if(!n)
break;
}
else
{
printf("%3d%3d",p->num,q->num);
p->flag=0;
q->flag=0;
n-=2;
if(!n)
break;
}
while(1)
{
if(p->flag)
break;
p=p->hou;
}
while(1)
{
if(q->flag)
break;
q=q->qian;
}
if(n)
putchar(',');
}
putchar('\n');
}
return 0;
}