Josephus问题
我的解法:
#include <iostream> #include <list> using namespace std; int main() { int i,j,n,m,mPrime,numLeft; list<int> L; list<int>::iterator iter; //初始化 cout<<"enter N(# of people)&M(# of passes before elimination)"; cin>>n>>m; numLeft=n; mPrime=m%n; for(i=1;i<=n;i++) { L.push_back(i); } iter=L.begin(); for(i=0;i<n;i++) { mPrime=m%numLeft; for(j=0;j<mPrime;j++) { iter++; if(iter==L.end()) iter=L.begin(); } cout<<*iter<<" "; iter=L.erase(iter); numLeft-=1; if(iter==L.end()) iter=L.begin(); } cout<<endl; return 0; }