码迷,mamicode.com
首页 > 其他好文 > 详细

UVa133

时间:2016-08-17 01:36:15      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

The Dole Queue 

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.

题意:

       有N个人围成一圈、给其中任意一人编号为1,顺时针依次从1到N给剩余的每个人编号。N号应该就在1号的旁边。现在每回合不断选走一个或者两个人,知道没有人剩下,规则是这样的:首先从1号开始每次顺时针地数k个人(包括1号)并选定第k个人,然后从N号开始逆时针地数m个人(包括N号)并选定第m个人,如果前面选定的那个人与后面选定的那个人重合,则选走那一个人,否则选走分别选定的两个人。每次都是顺时针数k个人和逆时针数m个人,只不过都从分别选定的那个人的下一个人开始数起。按顺序输出每次被选走的人。

 

输入:

       多组数据,每组给出N,k,m。以0,0,0作为输入的结束。

输出:

       每组数据按顺序输出每次出圈人的编号,次与次之间有逗号间隔,整数都要占3个空格的位置并右顶格。

分析:

       模拟题。每次遍历整个环形数列,如果已经选走则将它置为零。

技术分享
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 const int MAX_N = 20;
 8 int n,k,m;
 9 int arr[MAX_N + 1];
10 int main(){
11     while(scanf("%d%d%d",&n,&k,&m) == 3){
12         if(!n && !k && !m) break;
13         int size = n;
14         int p = 0,q = n - 1;
15         for(int i = 0 ; i < n ; i++) arr[i] = i + 1;
16         while(size > 0){
17             for(int i = 1 ; i <= k - 1 ; i++){
18                 p = (p + 1) % n;
19                 if(!arr[p]) i--;
20             }
21             for(int j = 1 ; j <= m - 1 ; j++){
22                 q = (q - 1 + n) % n;
23                 if(!arr[q]) j--;
24             }
25             printf("%3d",arr[p]);
26             arr[p] = 0;
27             size--;
28             if(arr[q]){
29                 printf("%3d",arr[q]);
30                 arr[q] = 0;
31                 size--;
32             }
33             for(int i = 0 ; i < n ; i++){
34                 p = (p + 1) % n;
35                 if(arr[p]) break;
36             }
37             for(int j = 0 ; j < n ; j++){
38                 q = (q - 1 + n) % n;
39                 if(arr[q]) break;
40             }
41             printf("%c",size == 0 ? \n : ,);
42         }
43     }
44     return 0;
45 }
View Code

 

UVa133

标签:

原文地址:http://www.cnblogs.com/cyb123456/p/5778354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!