/*Joseph Circle*/
#include
#include
#include
typedef struct Node
{ int order;
int password;
struct Node *next;
}Node,*Linklist;
Linklist init_circle(int n,int s[],Linklist L);
void out_circle(int n,int m,int out[],Linklist L);
void print_out(int n,int out[]);
void exit_circle();
main()
{ char ch;
Linklist L,q;
int i,m,n,s[100],out[100],count=0;
printf("---------------------------------Joseph Circle---------------------------------\n");
while(1)
{ printf("Please input the total number of persons(n):");
scanf("%d",&n);
while(1)
{ if(n>0) break;
printf("Error!Please enter again(n>0)");
scanf("%d",&n);
if(++count==3) exit_circle();
}
printf("Please input everyone's password in order:\n");
count=0;
while(1)
{ int ct=0;
for(i=1;i<=n;i++)
{ scanf("%d",&s[i]);
if(s[i]<=0) ct++;
}
if(ct==0) break;
if(++count==3) exit_circle();
printf("Error!Please enter again(password>0)\n:");
}
printf("Please input the initial value(m):");
scanf("%d",&m);
count=0;
while(1)
{ if(m>0) break;
printf("Error!Please enter again(m>0)");
scanf("%d",&m);
if(++count==3) exit_circle();
}
q=init_circle(n,s,L);
out_circle(n,m,out,q);
print_out(n,out);
printf("\nContinue or Not(Y/N)?");
getchar();
scanf("%c",&ch); /*此处还有问题!!!!!!*/
if(!(ch=='y'||ch=='Y'))
exit_circle();
}
}
Linklist init_circle(int n,int s[],Linklist L)
{ int i;
Linklist p,q;
L=(struct Node*)malloc(sizeof(struct Node));
L->order=1;
L->password=s[1];
q=L;
for(i=1;i<=n-1;i++)
{ p=(struct Node*)malloc(sizeof(struct Node));
q->next=p;
p->order=i+1;
p->password=s[i+1];
q=p;
}
q->next=L;
return q;
}
void out_circle(int n,int m,int out[],Linklist r)
{ int i;
Node *p,*q;
for(i=1;i<=n;i++)
{ while(m!=1)
{ r=r->next;
m--;
}
p=r->next;
out[i]=p->order;
m=p->password;
q=p->next;
free(p);
p=q;
r->next=p;
}
}
void print_out(int n,int out[])
{ int i;
printf("The sequence is:\n");
for(i=1;i<=n;i++)
printf("%-7d",out[i]);
}
void exit_circle()
{
printf("Welcome to use our system!Thank you!");
//getch();
exit(0);
}
阅读(516) | 评论(0) | 转发(0) |