救济金发放问题,N数按照逆时针排成一圈,一个指针从1到N逆时针数到第k个数,另一个指针从N到1数m个数,将数到的数取出,然后重复这个过程。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N, k, m;
int pFst;
int pSnd;
int cnt;
while (cin >> N >> k >> m) {
if (N == 0 || k == 0 || m == 0)
break;
cnt = N;
pFst = 0;
pSnd = N - 1;
bool *cArr = new bool[N];
for (int i = 0; i < N; ++i)
cArr[i] = true;
while (cnt > 0) {
int temp = 0;
while (1) {
if (cArr[pFst] == false) {
pFst = (pFst + 1) % N;
continue;
}
else {
temp++;
if (temp == k )
break;
else
pFst = (pFst + 1)%N;
}
}//endwhile
temp = 0;
while (1) {
if (cArr[pSnd] == false) {
pSnd = (pSnd + N - 1) % N;
continue;
}
else {
temp++;
if (temp == m)
break;
else
pSnd = (pSnd + N-1) % N;
}
}//endwhile
if (pFst == pSnd) {
cnt--;
cout << setw(3) << pFst + 1;
if (cnt != 0)
cout << ",";
cArr[pFst] = false;
}
else {
cout << setw(3) << pFst + 1;
cout << setw(3) << pSnd + 1;
cnt -= 2;
if (cnt != 0)
cout << ",";
cArr[pFst] = false;
cArr[pSnd] = false;
}
pFst = (pFst + 1) % N;
pSnd = (pSnd - 1 + N) % N;
}//endwhile
cout << endl;
}//endwhile
return 0;
}