- #include<iostream.h>
- #include <malloc.h>
- #define n 10
- typedef struct
- {
- int data[10];
- int length;
- }sqcircl;
- void initcircl(sqcircl *&c)//初始化约瑟夫环
- {
- c=(sqcircl *)malloc(sizeof(sqcircl));
- c->length=0;
- }
- void encircl(sqcircl *&c,int a[])//创建约瑟夫环
- {
- sqcircl *s=c;
- int i;
- for(i=0;i<10;i++)
- {
- s->data[i]=a[i];
- c->length++;
- }
- }
- void decircl(sqcircl *&c,int m)//寻找报数为m的人并将其叫出列
- {
- sqcircl *s=c;
- int i,j,count=0;
- for(j=0;j<10;j++)
- {
- for(i=0;i<10;i++)
- {
- if(s->data[i]!=0)
- count++;
- if(count==m)
- {
- cout<<s->data[i]<<" ";
- count=0;
- s->data[i]=0;
- s->length--;
- }
- }
- s=c;
- }
- cout<<endl;
- }
- void main()
- {
- sqcircl *c;
- int a[n]={1,2,3,4,5,6,7,8,9,10},m;
- initcircl(c);
- encircl(c,a);
- cout<<"请输入要出列的报数:";
- cin>>m;
- cout<<"依次出队的序列:"<<endl;
- decircl(c,m);
- }