In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).
Sample input
10 4 3 0 0 0
Sample output
4
8,
9
5,
3
1,
2
6,
10,
7
where represents a space.
就是一堆人站成一个环,一个人从第一个人顺时针数,一个人从最后一个人逆时针数,数到同一个人就出来一个,不同的人就出来两个,要求输出过程。和约瑟夫环是一样的,我手动写了两个数组标记模拟,先找出选中的两个人,判断是否一个人,一直循环到没有人为止。
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int leap1[30], leap2[30];
int main()
{
int i, j, m, n, ans, t, k, temp1, temp2, num, pre1, pre2;
while (scanf("%d%d%d", &n, &k, &m) != EOF)
{
if (n == 0 && k == 0 && m == 0)break;
memset(leap1, 0, sizeof(leap1));
memset(leap2, 0, sizeof(leap2));
num = n; pre1 = pre2 = 0;
while (num)
{
ans = 0; i = 0;
for (i = 0; ans < k; i++)
{
if (!leap1[(pre1 + i) % n])
{
ans++;
}
if (ans == k)
{
temp1 = (pre1 + i) % n;
pre1 = temp1;
}
}
ans = 0; i = 0;
for (i = 0; ans < m; i++)
{
if (!leap2[(pre2 + i) % n])
{
ans++;
}
if (ans == m)
{
temp2 = n - (pre2 + i) % n;
pre2 = (pre2 + i) % n;
}
}
temp1++;
if (temp1 == temp2)
num--;
else num = num - 2;
leap1[temp1 - 1] = 1;
leap2[n - temp1] = 1;
leap2[n - temp2] = 1;
leap1[temp2 - 1] = 1;
if (temp2 == -1)
{
if (temp1 < 10)
cout << " " << temp1;
else
cout << " " << temp1;
}
else if (temp1 == temp2)
{
if (temp1 < 10)
cout << " " << temp1;
else
cout << " " << temp1;
}
else
{
if (temp1 < 10)
cout << " " << temp1;
else
cout << " " << temp1;
if (temp2 < 10)
cout << " " << temp2;
else
cout << " " << temp2;
}
if (num)
cout << ",";
}
cout << endl;
}
return 0;
}
写的非常搓,看了刘汝佳的方法,感觉很简便,尝试学习一下,调试了才发现他写的暗含很多道理,比如顺时针开始从n开始,逆时针是1开始,主要是函数用得好,看起来简便多了。
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n, k, m, a[25];
int Go(int p,int d,int m)
{
if (a[p] != 0)
m--;
while (m--)
{
do{
p = (p + d + n) % n;
} while (a[p] == 0);
}
return p;
}
int main()
{
int i, j;
while (scanf("%d%d%d", &n, &k, &m) != EOF)
{
if (n == 0 && k == 0 && m == 0)break;
for (i = 0; i < n; i++)
a[i] = i + 1;
int left = n;
int p1, p2;
p1 = 0;
p2 = n - 1;
while (left)
{
p1 = Go(p1, 1, k);
p2 = Go(p2, -1, m);
printf("%3d", p1+1); left--;
if (p1 != p2)
{
printf("%3d", p2+1); left--;
}
if (left)
cout << ",";
a[p1] = a[p2] = 0;
}
cout << endl;
}
return 0;
}