您的位置:首页 > 产品设计 > UI/UE

Uva 133 The Dole Queue 双向约瑟夫环

2014-07-21 09:26 393 查看
C - The Dole Queue
Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Description
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.

题目大意:

(1) N个人围成一圈。随便指定一个位置为1,然后依次逆时针标记为1,2,...,N。

(2) 一个人从1开始数到 k,这个人出列,为A[i],另一个人同时从 N数 m个数,即 N-m出列,记为B[i]。

(3) 因为是同时计数的,所以可能出现都标记到同一个人,这时A[i] = B[i]。只输出一个即可。

否则输出时都是成对输出,然后输出一个 "," 当然末尾没有"," (当然 如果A[i] = B[i]就只输出一个数了)

注意:让两个人都找到A[i],B[i] 后再标记A[i],B[i]已经访问,不然无法得到正确输出。

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
string str;

void init(int n) {
	str = "";
	char ch;
	for(int i = 1;i <= n;i++) {
		ch = 1;
		str += ch;
	}
}

void solve(int n,int l,int r) {
	int cut_l = 0;
	int cut_r = 0;
	int cut = 0;
	int i=0,j=n-1;
	int flag1,flag2;
	while(cut < n) {
		while(cut_l < l ) {
			if(str[i] != 0)
				cut_l++; 
			if(cut_l == l)
				flag1=i;
			if(i < n-1)
				i++;
			else
				i=0;
		}
		while(cut_r < r) {
			if(str[j] != 0)
				cut_r++;
			if(cut_r == r)
				flag2 = j;
	        if(j > 0)
				j--;
			else
				j=n-1;
		}
		if(flag1 == flag2) {
			str[flag1] = str[flag2] =0;
			cut++;
			printf("%3d",flag1+1);
		}
		else{
			str[flag1] = str[flag2] = 0;
			cut += 2;
			printf("%3d%3d",flag1+1,flag2+1);
		}
		if(cut != n) {
			cout<<',';
		}
		cut_l=0,cut_r=0;
	}
}
int main() {
	int n,l,r;
	while(cin >> n >> l >> r && (n || l || r)) {
		init(n);
		solve(n,l,r);
		cout<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: