题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69

13874119 133 The Dole Queue Accepted C++ 0.009 2014-07-13 02:44:49

 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.


解题思路:一道非常类似约瑟夫问题的题目。http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1197
     (关于约瑟夫问题,可以翻阅《具体数学》第一章引例。)

     直接数组模拟就好,没特殊机巧。白书上标程的写法更加精炼,要多学学优化自身的代码!

 #include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int maxn = ;
int n, m, k, peo[maxn]; int solve_z(int cur_first, int step_time) {
int ans = cur_first;
while (step_time --) {
ans ++;
if (ans > n) ans = ;
while (peo[ans] == -) {
ans ++;
if (ans > n) ans = ;
}
}
return ans;
} int solve_f(int cur_first, int step_time) {
int ans = cur_first;
while (step_time --) {
ans --;
if (ans == ) ans = n;
while (peo[ans] == -) {
ans --;
if (!ans) ans = n;
}
}
return ans;
} int main() {
while (cin >> n >> k >> m) {
if (n + k + m == ) break;
for (int i = ; i <= n; i ++) {
peo[i] = i;
} int left_peo = n;
int cur1 = n, cur2 = ;
while (left_peo != ) {
cur1 = solve_z(cur1, k);
//cout << cur1 << endl;
cur2 = solve_f(cur2, m);
//cout << cur2 << endl;
//system("pause");
printf("%3d", cur1); left_peo --;
if (cur2 != cur1) {
printf("%3d", cur2);
left_peo --;
}
//cout << ",";
peo[cur1] = peo[cur2] = -;
if(left_peo) cout << ",";
}
cout << endl;
} return ;
}

UVa133.The Dole Queue的更多相关文章

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

    题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...

  2. 【紫书】uva133 The Dole Queue 参数偷懒技巧

    题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...

  3. UVA133 - The Dole Queue【紫书例题4.3】

    题意: n个人围成个圆,从1到n,一个人从1数到k就让第k个人离场,了另一个人从n开始数,数到m就让第m个人下去,直到剩下最后一个人,并依次输出离场人的序号. 水题,直接上标程了 #include&l ...

  4. UVA 133 The Dole Queue

    The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...

  5. The Dole Queue

    The Dole Queue Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid ...

  6. 水题:UVa133-The Dole Queue

    The Dole Queue Time limit 3000 ms Description In a serious attempt to downsize (reduce) the dole que ...

  7. The Dole Queue UVA - 133

     In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros ...

  8. uva - 133 The Dole Queue(成环状态下的循环走步方法)

    类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...

  9. UVA 133 The Dole Queue(报数问题)

    题意:一个长度为N的循环队列,一个人从1号开始逆时针开始数数,第K个出列,一个人从第N个人开始顺时针数数,第M个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...

随机推荐

  1. day55

    担心了好久的编译原理也总是考完了 大学里的最后一次考试也是结束罗 这次的考试起伏跌宕啊 我们本来是9点钟开始考试 但是我们班的几个同学基本上7点钟就去了 为了什么?? 选个好的位置撒哈哈,到了九点,老 ...

  2. mybatis学习笔记第一讲

    第一步:先配置mybatis配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE confi ...

  3. js获取url传递参数(转的,原作不详)

    这里是一个获取URL带QUESTRING参数的JAVASCRIPT客户端解决方案,相当于asp的request.querystring,PHP的$_GET 函数: <Script languag ...

  4. 优化移动体验的HTML5技巧

    简介 连轴转的刷新,不断变向的页面转换,以及tap事件的周期性的延迟仅仅是现在移动web环境令人头疼事情的一小部分.开发者正试图尽可能的靠近原生应用,但却经常被各种兼容问题,系统复位,和僵化的框架打乱 ...

  5. 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询

    我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...

  6. leetcode_question_57 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. Android自定义控件(四)——让每一个Activity UI都具有弹性

    前面我们已经介绍了如何让你的ScrollView,ListView具有弹性, 今天,我们在前面的基础上,做一下适当的修改,让那些既不是ScrollView,也不是ListView的Activity页面 ...

  8. Stm32高级定时器(三)

    Stm32高级定时器(三) 1 互补输出和死区插入 1.1 死区:某个处于相对无效状态的时间或空间 本来OCX信号与OCXREF时序同相同步,OCXN信号与OCXREF时序反相同步.但为了安全考虑,以 ...

  9. python-socket 粘包问题

    解决粘包的问题: 1.服务端在发送数据之前,先把发送数据的长度告诉客户端,要发送多少数据,然后客户端根据这个数据的长度循环接收就OK 传输过程: 服务端:     1.send  #数据长度     ...

  10. iOS图片设置圆角

    一般我们在iOS开发的过程中设置圆角都是如下这样设置的. imageView.clipsToBounds = YES; [imageView.layer setCornerRadius:]; 这样设置 ...