约瑟夫环

#include <iostream>
using namespace std;
struct student
{
int num;
int mima;
student *next;
};
int getn()
{
int n;
cout<<"please enter the number of person:";
cin>>n;
return n;
}

student *create(int n)
{
student *head;                    
student *ps;

int j=1;
ps=new student;
cout<<"please enter the secret of each person:";
cin>>ps->mima;
ps->num=j;
j++;
student *pt=ps;
head=pt;

int i;
while(n>1){
for(i=0;i<n-1;i++)
{
ps=new student;
cin>>ps->mima;
ps->num=j;

j++;
pt->next=ps;
pt=ps;
}
pt->next=head;
if(i==n-1)break;
}
return pt;
delete ps;
}
void print(student *pt, int n)
{
student *p,*q,*r;                       
p=pt;

cout<<"please enter the number of m:";
int m,j;
cin>>m;
int i;
   for(i=0;i<n;i++){
    if(n==1){
     cout<<p->num<<" " ;delete p;
    }
    else{

     for(j=0;j<m;j++){
   q=p;
   p=p->next;
}
m=p->mima;
cout<<p->num<<" ";
q->next=p->next;
r=p;
p=q;
delete r;
    }
   }
}
int main()
{
int n;
n=getn();
student *p;
p=create(n);
print(p,n);
return 0;
}

你可能感兴趣的:(J#)