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个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...
随机推荐
- euctb
- 什么是 docker?
关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...
- Git 2.7: 一个新的带来许多新特性和性能提升的主要版本
在2.6版本发布两个月之后,Git 2.7发布.该版本带来了许多新特性以及性能的提升. 本文选取了Git 2.7带来的主要变化: git remote支持get-url子命令,可以显示指定远端的URL ...
- jsonp+handler 的实现
//参考 http://www.cnblogs.com/yuwensong/archive/2013/05/28/3103064.html 后台: public void ProcessRequest ...
- Java基础 -- 冒泡排序算法(带详细注释)
冒泡排序的要点: 1.多轮排序,每轮排序中选出最大的元素放在最顶端,并且下次排序不再使用该元素; 2. 使用双for循环,外层for循环控制要排序的次数(轮数), 内层for循环控制当前要排序的元素并 ...
- C++内存泄露检測原理
转自:http://hi.baidu.com/jasonlyy/item/9ca0cecf2c8f113a99b4981c 本文针对 linux 下的 C++ 程序的内存泄漏的检測方法及事实上现进行探 ...
- 在CentOS 7上利用systemctl加入自己定义系统服务
CentOS 7继承了RHEL 7的新的特性,比如强大的systemctl,而systemctl的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的执行效率 ...
- ASP.NET MVC请求处理过程
- Java数据结构漫谈-Vector
List除了ArrayList和LinkedList之外,还有一个最常用的就是Vector. Vector在中文的翻译是矢量,向量,所以大家喜欢把Vector叫做矢量数组,或者向量数组. 其实就底层实 ...
- FineUI控件之树的应用(二)
一.Tree控件应用 <f:PageManager ID="PageManager1" runat="server" /> <f:Tree I ...