uva133-S.B.S.

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.

----------------------------------我是分割线--------------------------------------------

这道题简单,上代码:

 1 // UVa133 The Dole Queue
 2 #include<cstdio>
 3 #define maxn 25
 4 int n, k, m, a[maxn];
 5 int go(int p, int d, int t) {
 6   while(t--) {
 7     do {p=(p+d+n-1)%n+1;} while(a[p] == 0);
 8   }
 9   return p;
10 }
11
12 int main() {
13   while(scanf("%d%d%d", &n, &k, &m) == 3 && n) {
14     for(int i = 1; i <= n; i++) a[i] = i;
15     int left = n;
16     int p1 = n, p2 = 1;
17     while(left) {
18       p1 = go(p1, 1, k);
19       p2 = go(p2, -1, m);
20       printf("%3d", p1); left--;
21       if(p2 != p1) { printf("%3d", p2); left--; }
22       a[p1] = a[p2] = 0;
23       if(left) printf(",");
24     }
25     printf("\n");
26   }
27   return 0;
28 }
时间: 2024-10-05 04:58:34

uva133-S.B.S.的相关文章

Uva133 - The Dole Queue

题意 n个人围城一个环,逆时针编号1~n,A从1开始逆时针数K个,B从n顺时针数M个,被选中的1或2个人一次领救济金,输出顺序 思路 双向约瑟夫环,公式没推出来,用的模拟 总结 开始用的数组模拟链表写的,WA了,打算再改改.不知道为什么这道题卡了很久总不对. 模拟: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using nam

UVa133

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

uva133 The Dole Queue ( 约瑟夫环的模拟)

题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+n-1)%n+1的妙用... 题目:  The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decid

救济金发放(UVa133)

题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69 C++11代码如下: 1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int que[25]; 5 int n, k, m; 6 7 int go(int p, i

算法习题---4.3救济金发放(UVa133)

一:题目 (n<20 )个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上).接下来被选中的人离开队伍. 输入n,k,m输出每轮被选中的人的编号(如果有两个人,先输出A的)每个输出数字正好占3列. 二:实现思路 A从数组首部向后遍历(若是该位置还有人,则步数加一,否则不改变步数),当遍历到最后,则转回首部继续遍历. B从数组尾部向前遍历(...),若是遍历到首部,则转回尾部

算法练习(一)

昨天是个值得纪念的日子,我数学建模拿了推荐国家一等奖的名额,希望最后能顺利拿到国一吧.现在大三已经开学一个月了.这一个月因为社会实践评优的事情真的很忙,还好最后拿到了可能拿到的所有的奖项.结果自己把科研助手这件事给耽误了,今天去找马老师,结果马老师的实验室人已经满了.所以没办法,我可能又要去找其他老师了. 今年国家奖学金的名额里面没有我,没有就算了吧.卧薪尝胆,好好学习,这一学期至关重要.所以自己现在就要开始准备保研的机试,现在的训练非常重要,无论如何,这是自己未来要走的一步路.我现在最重要的就

引用,引用形参,指针形参与指向指针的引用形参,内存泄露及free相关

由做UVa133引发的一系列问题及讨论 1.引用类型    C++ Primer P51 引用就是对象的另一个名字,使用多个变量名指向同一地址.实际程序中,引用主要用作函数形参. 复合类型.不能定义引用类型的引用,但可以定义任何其他类型的引用. 格式: 类型名&  标示符=已声明的常变量; 2.引用形参    C++ Primer P201-205 非引用形参有普通的.指针形参.const形参(可传const对象或非const对象) 引用形参,3种情形:@修改实参,或返回多个值  @避免复制大型

NOJ AC50记录

NOJ刷题总结 +++ HDU1969 Pie #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define P acos(-1.0) using namespace std; const int N = 10010; int n, f, a[N]; bool more_pie(double u) { i

算法竞赛入门经典 第四章

[√ ] UVA1339 古老的密码 Ancient Cipher [√ ] UVA489 刽子手的游戏 Hangman Judge [√ ] UVA133 救济金发放 The Dole Queue [√ ] UVA213 信息解码 Message Decoding [√ ] UVA512 追踪电子表格中的单元格 Spreadsheet Tracking [√ ] UVA12412 师兄帮帮忙 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)