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 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.
代码为:
#include<cstdio>
#include<cstring>
const int maxn=25+10;
bool vis[maxn];
int n,k,m; int Move(int pos,int flag,int step)
{
for(int i=1;i<=step;i++)
{
do
{
pos=(pos+flag+n-1)%n+1;
}while(vis[pos]);
}
return pos;
} int main()
{
int left,p1,p2;
while(~scanf("%d%d%d",&n,&k,&m))
{
if(n==0&&k==0&&m==0) return 0;
memset(vis,false,sizeof(vis));
p1=n;
p2=1;
left=n;
while(left)
{
p1=Move(p1,1,k);
p2=Move(p2,-1,m);
printf("%3d",p1);
left--;
if(p1!=p2)
{
printf("%3d",p2);
left--;
}
vis[p1]=vis[p2]=1;
if(left)
printf(",");
else
printf("\n");
}
}
return 0;
}
Sample output
4
8,
9
5,
3
1,
2
6,
10,
7
uva133 The Dole Queue ( 约瑟夫环的模拟)的更多相关文章
- UVa133.The Dole Queue
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【紫书】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 ...
- 约瑟夫环(Josehpuse)的模拟
约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...
- Roman Roulette(约瑟夫环模拟)
Roman Roulette Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 4841 圆桌问题(用vector模拟约瑟夫环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841 圆桌问题 Time Limit: 3000/1000 MS (Java/Others) M ...
- 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...
- tc 147 2 PeopleCircle(再见约瑟夫环)
SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...
- 14.约瑟夫环问题[JosephusProblem]
[题目] n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始,每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后,从被删除数字的下一个继续删除 ...
随机推荐
- hdu5172(线段树)
传送门:GTY's gay friends 题意:判断区间[l,r]内的数字是否符合1~len(r-l+1)的一个全排列. 分析:pos[i]记录数字i出现的最大位置,pre[i]记录在位置i的数字a ...
- 基于CefGlue的桌面应用开发
原文地址:http://johnnyfee.github.io/csharp/2013/12/21/cef-glue/ 前言 如果你想使用WEB技术来开发桌面客户端,并且是想使用的语言也是C#时,那请 ...
- JQuery是继prototype之后又一个优秀的Javascript库
JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Oper ...
- 基于 Apache Mahout 构建社会化推荐引擎
基于 Apache Mahout 构建社会化推荐引擎 http://www.ibm.com/developerworks/cn/views/java/libraryview.jsp 推荐引擎利用特殊的 ...
- C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans
Lable的Left属性是能够更改的,可是 Right属性不能够更改,所以我们能够利用 这个特点做自加 自减运算 using System; using System.Collections.Gene ...
- maven仓库总结,maven私服搭建
配置pom.xml依赖包时在这里找包的描述: http://search.maven.org/#browse 以java为根目录. mvn archtype:generate -DgroupId=zt ...
- C++结构体:默认构造函数,复制构造函数,重载=运算符
C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数,运算符重载,这些功能使得结构体对象能够方便的传值. 比如,我定义一个简单的结构体,然后将其作为vector元素类型,要使用的话,就 ...
- HDU1194_Beat the Spread!
Beat the Spread! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ 1146:ID Codes
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...
- Windows Phone开发(4):框架和页
原文:Windows Phone开发(4):框架和页 在开如之前,我想更正一个小问题,之前我在第一篇文章中说,Visual Studio Express for Windows Phone的中文版没有 ...