问题描述: 设有N个人围坐一圈并按顺时针方向从1到N编号,从第S个人开始进行1到M报数,报数到第M个人时,此人出圈,再从他的下一个人重新开始1到M的报数,如此进行下去直到所有的人都出圈为止。现要打印出出圈次序。 输入:n 游戏总人数 s 报数的起始编号 m 报数的数值 输出:p 指向长度为n的数组,出圈次序保存在p指向的数组中 示例 n=7 s=2 m=3 出圈次序为:4 7 3 1 6 2 5 n=3 s=1 m=2 出圈次序为:2 1 3 #include <iostream> #include <vector> using namespace std; /* n=7 s=2 m=3 出圈次序为:4 7 3 1 6 2 5 n=3 s=1 m=2 出圈次序为:2 1 3 */ void process(int n,int s,int m) { if(n<=0 || (s<1 || s>n) || m<=0) { cout<<"E8"<<endl; return; } int MAX=n; int start=s; int step=m; vector<int> men; vector<int> outputList; men.clear(); outputList.clear(); for (int i=0;i<MAX;i++) men.push_back(i+1); start=start-1; int k=0; while(!men.empty()) { k=(start+step-1)%MAX; outputList.push_back(men[k]); men.erase(men.begin()+k); MAX=men.size(); if (!MAX) break; start=k%MAX; } for (vector<int>::iterator it=outputList.begin();it!=outputList.end();it++) cout<<*it<<" "; cout<<endl; } int main() { int n,s,m; while(cin>>n>>s>>m) { process(n,s,m); } return 0; }