标签:
题目:给你一串数字按照环形排列,每次向后数k个向前数m个的删掉,如果相同只删掉一个,输出删数字的顺序。
分析:约瑟夫环变形,模拟。直接模拟过程输出即可。
说明:注意%3d输出即可。
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int people[21]; int main() { int n,k,m,left,right,last,count; while (cin >> n >> k >> m && n) { left = 1;right = 0;last = n; memset(people, 0, sizeof(people)); while (last) { count = 0; while (count < m) { -- left; if (left < 1) left = n; if (!people[left]) ++ count; } count = 0; while (count < k) { ++ right; if (right > n) right = 1; if (!people[right]) ++ count; } people[left] = people[right] = 1; if (left != right) printf("%3d%3d",right,left); else printf("%3d",left); last -= 1+(right!=left); if (last) printf(","); }printf("\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/45544207