The Dole Queue

本文介绍了一种基于约瑟夫环问题的实现方案,通过两种不同的编程方法展示了如何处理一组人员按照特定规则被选择出去的过程。其中包括使用标记数组进行模拟的直接方法,以及采用更高效、简洁的函数递归调用来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

tex2html_wrap_inline34 4 tex2html_wrap_inline34 8, tex2html_wrap_inline34 9 tex2html_wrap_inline34 5, tex2html_wrap_inline34 3 tex2html_wrap_inline34 1, tex2html_wrap_inline34 2 tex2html_wrap_inline34 6, tex2html_wrap_inline50 10, tex2html_wrap_inline34 7

where tex2html_wrap_inline50 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值