UVa133.The Dole Queue
题目链接: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的更多相关文章
- uva133 The Dole Queue ( 约瑟夫环的模拟)
题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...
- 【紫书】uva133 The Dole Queue 参数偷懒技巧
题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...
- UVA133 - The Dole Queue【紫书例题4.3】
题意: n个人围成个圆,从1到n,一个人从1数到k就让第k个人离场,了另一个人从n开始数,数到m就让第m个人下去,直到剩下最后一个人,并依次输出离场人的序号. 水题,直接上标程了 #include&l ...
- UVA 133 The Dole Queue
The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...
- The Dole Queue
The Dole Queue Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit cid ...
- 水题:UVa133-The Dole Queue
The Dole Queue Time limit 3000 ms Description In a serious attempt to downsize (reduce) the dole que ...
- The Dole Queue UVA - 133
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros ...
- uva - 133 The Dole Queue(成环状态下的循环走步方法)
类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...
- UVA 133 The Dole Queue(报数问题)
题意:一个长度为N的循环队列,一个人从1号开始逆时针开始数数,第K个出列,一个人从第N个人开始顺时针数数,第M个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...
随机推荐
- java算法之身份证号码验证
调用时直接 new IDCard().verify(身份证id);就可以了 实现代码如下: public class IDCard { private String _codeError; //wi ...
- phpcms:四、尾部包含
四.尾部包含1.包含尾部文件:{template "content","footer"}2.栏目列表调用(关于我们| 联系方式| 版权声明| 招聘信息|):{p ...
- Ubuntu/Linux 笔记应用 为知笔记(支持markdown)
发现网易云笔记没有Linux,但是为知笔记有Linux版本,且支持markdown格式 sudo add-apt-repository ppa:wiznote-team sudo apt-get up ...
- [Angular 2] Using events and refs
This lesson shows you how set listen for click events using the (click) syntax. It also covers getti ...
- 初识QML学习机制
在QML中,一个用户界面被指定为具有属性的对象树,这使得Qt更加便于很少或没有编程经验的人使用,JavaScript在QML中作为一种脚本语言,对QML进行逻辑方面的编程. AD:WOT2015 互联 ...
- 代码,显示IPhone剩余磁盘空间
#include <sys/mount.h> //这段代码示范怎么取得 iPhone 的剩余磁盘空间,还有全部磁盘空间 long long freeSpace() { struct sta ...
- Javascript进阶篇——(DOM—认识DOM、ByName、ByTagName)—笔记整理
认识DOM文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 将HTML代码分解 ...
- update-database时出现Cannot attach the file
在进行Migrations时,如果直接删除了Db文件,在使用update-database时会出现Cannot attach the file发问题 解决方案:
- 开源项目Material Calendar View 学习记录 (一)
开源项目Material Calendar View 学习记录 Github: https://github.com/prolificinteractive/material-calendarview ...
- 关于DIPS的MVC 4.0项目发布与在IIS 7.0上的部署的方法
本人技术笨拙,今天在发布DIPS的MVC4.0项目,并部署到IIS上,遇到各种问题.在查询相关资料后,最终得以解决,所以想把这个过程记录下来. 首先是MVC4.0项目的发布: 打开你的VS2012网站 ...