//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //方法二:結構的應用 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include <iostream.h> #include <iomanip.h>
struct jose { int code; jose *next; };
void mian() { int numOfBoys,interval; cout<<"Please input the number of boys," <<endl <<"interval of counting:"; cin>>numOfBoys>>interval;
jose *pJose=new jose[numOfBoys]; jose *pCurrent=pJose;
int itemsInLine=0;
for(int i=1;i<=numOfBoys;i++) { pCurrent->next=pJose+i%numOfBoys; pCurrent->code=i; pCurrent=pCurrent->next;
if(itemsInLine++%10==0) cout<<endl; cout<<setw(4)<<i; }
itemsInLine=0;
int numOfCount; cout<<"please input the number of count:" <<endl; while(1) { cin>>numOfCount; if(0<numOfCount&&numOfCount<=numOfBoys) break; }
jose *pivot; pCurrent=&pJose[numOfCount-1];
while(pCurrent->next!=pCurrent) { for(int j=0;j<interval;j++) { pivot=pCurrent; pCurrent=pivot->next; }
if(itemsInLine++%10==0) cout<<endl; cout<<setw(4)<<pCurrent->next; pivot->next =pCurrent->next; pCurrent=pivot; }
cout<<endl <<"the winner is:" <<pCurrent->code <<endl;
delete []pJose;
}
|