您的位置:首页 > 其它

Joseph队列解法

2015-06-02 22:48 211 查看
int main(){

while(true){
int a = 0, b = 0;
int numOfPeople = 0, numToSpare = 0;
cout<<"input number of people:";
cin>>numOfPeople;
cout<<"input number to spare per circle:";
cin>>numToSpare;

determinSurvivor(numOfPeople, numToSpare, a, b);
cout<<"the last two person to be topilled is "<<a<<" and "<<b<<endl;

if(stopCircle())
break;
}
return 0;
}

bool stopCircle(void){
string flag;
while(true){
cout<<"Do you want to try another file? ";
flag = getLine();
if( flag == "yes" )
return false;
else if( flag == "no" )
return true;
cout<< "Please answer 'yes' or 'no' "<<endl;
}
}

void determinSurvivor(int numOfPeople, int numToSpare, int& last, int& nextToLast){
Queue<int> circle;
for(int i = 1; i <= numOfPeople; i++)
circle.enqueue(i);
while(!circle.isEmpty()){
for(int j = 0; j < numToSpare; j++){
int next = circle.dequeue();
circle.enqueue(next);
}
nextToLast = last;
last = circle.dequeue();
cout<<"the "<<last<<" person should be topilled"<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: