题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69
C++11代码如下:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int que[25]; 5 int n, k, m; 6 7 int go(int p, int d, int t) { //d表示步长,值为1表示逆时针,-1表示顺时针 8 while (t--) { 9 do { 10 p = (p + d + n - 1) % n + 1; //防止(p+d)%n==0,(p+d+n-1)相当于旋转一周,最后加1 11 } while (que[p]==0); 12 } 13 return p; 14 } 15 int main() { 16 while ((cin >> n >> k >> m) && n != 0) { 17 for (int i = 1; i <= n; i++) que[i] = i; 18 int left = n; //剩下的人数 19 int p1 = n, p2 = 1; //起始位置 20 while (left) { 21 p1 = go(p1, 1, k); 22 p2 = go(p2, -1, m); 23 cout << setw(3) << p1; 24 left--; 25 if (p1 != p2) { 26 cout << setw(3) << p2; 27 left--; 28 } 29 que[p1] = que[p2] = 0; 30 if (left) cout << ','; 31 } 32 cout << endl; 33 } 34 return 0; 35 }
原文链接: https://www.cnblogs.com/pgzhang/p/9260449.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276985
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!