水题:UVa133-The Dole Queue
The Dole Queue
Time limit 3000 ms
Description
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 counterclockwise 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
Writeaprogramthatwillsuccessivelyreadin(inthatorder)thethreenumbers(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 counterclockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).
Note: The symbol ⊔ in the Sample Output below represents a space.
Sample Input
10 4 3 0 0 0
Sample Output
␣␣4␣␣8,␣␣9␣␣5,␣␣3␣␣1,␣␣2␣␣6,␣10,␣␣7
解题心得:
- 紫书上的例题,很简单可以直接模拟整个过程就行了,但是要注意输出的问题(”%3d”)。在一个功能的模块需要多次使用的时候可以直接写成函数,减少代码长度,不容易出错。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
bool cir[maxn];
int r,l,Len;
int _find(int pos,int len,int dir)//dir代表方向,1代表向右,0代表向左,pos是当前位置,len代表要走多少步
{
while(len)
{
if(dir == 1)
pos++;
else
pos--;
if(pos > Len)
pos = 1;
if(pos < 1)
pos = Len;
if(!cir[pos])
len--;
}
return pos;
}
int main()
{
int len,m,k;
while(scanf("%d%d%d",&len,&m,&k) && len+m+k)
{
Len = len;
memset(cir,false,sizeof(cir));
bool flag = false;
r = 0,l = Len+1;
while(len)
{
r = _find(r,m,1);
l = _find(l,k,0);
cir[r] = cir[l] = true;//true代表已经退出了的
if(r == l)
{
if(!flag)//flag用来控制,的输出
printf("%3d",r);
else
printf(",%3d",r);
len--;
}
else
{
if(!flag)
printf("%3d%3d",r,l);
else
printf(",%3d%3d",r,l);
len -= 2;
}
flag = true;
}
printf("\n");
}
return 0;
}
水题: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 ( 约瑟夫环的模拟)
题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...
- UVA133 - The Dole Queue【紫书例题4.3】
题意: n个人围成个圆,从1到n,一个人从1数到k就让第k个人离场,了另一个人从n开始数,数到m就让第m个人下去,直到剩下最后一个人,并依次输出离场人的序号. 水题,直接上标程了 #include&l ...
- 【紫书】uva133 The Dole Queue 参数偷懒技巧
题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...
- CF451C Predict Outcome of the Game 水题
Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- CF451A Game With Sticks 水题
Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second mem ...
- hdu1240 bfs 水题
原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...
- CCF 201612-1 最大波动 (水题)
问题描述 小明正在利用股票的波动程度来研究股票.小明拿到了一只股票每天收盘时的价格,他想知道,这只股票连续几天的最大波动值是多少,即在这几天中某天收盘价格与前一天收盘价格之差的绝对值最大是多少. 输入 ...
随机推荐
- h5复制粘贴板,打开APP功能
<div class="container"> <img src="../themes/mall/img/i_red_ad4.jpg"> ...
- 【翻译转载】【官方教程】Asp.Net MVC4入门指南(5):从控制器访问数据模型
在本节中,您将创建一个新的MoviesController类,并在这个Controller类里编写代码来取得电影数据,并使用视图模板将数据展示在浏览器里. 在开始下一步前,先Build一下应用程序(生 ...
- Echarts的重点
官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...
- Wrinkles should merely indicate where smiles have been.
Wrinkles should merely indicate where smiles have been. 皱纹应该只是微笑留下的印记.
- ubuntu和window之间如何共享文件
参考网上的自己动手实现共享文件: 1.打开虚拟机进入ubuntu系统,先安装增强功能包 2.安装完重启虚拟机后,在window下创建一个专门用来共享的文件夹 3.切换到ubuntu系统,在设备的共享文 ...
- 为什么HDFS的副本数通常选择3?
HDFS采用一种称为机架感知的策略来改进数据的可靠性.可用性和网络带宽的利用率. 在大多数情况下,HDFS的副本系数是3,HDFS的存放策略是一个副本存放在本地机架节点上,另一个副本存放在同一机架的另 ...
- 5分钟部署一个Hello World Servlet到CloudFoundry
首先从我的Github下载我写好的hello world Servlet到本地. 安装Maven,然后执行命令行mvn clean install,确保build成功,在项目根目录的target文件夹 ...
- 在浏览器里使用SAPGUI
事务码SICF,service name输入WEBGUI, 点右键,选择Test Service: 可以在浏览器里敲SE38进入ABAP editor了: 然么缺乏语法高亮显示: 如果想要浏览器里的语 ...
- 三维GIS-室内寻径功能实现
期末,要交一个大作业,正巧之前跑国图借书的时候,晕头转向的,国图内居然没有导航!!!借这个机会做一个室内导航的demo,只是半成品,还需要加入室内定位,匹配一下坐标才能在实际中使用. demo:利用蜂 ...
- cookie和session是否可以保存对象
session看了一下,是可以保存对象的.语法很普通,但是cookie的话本身是只能保存string类型的信息的,这就需要先序列化,然后接收的页面反序列化后形成对象调用,为了防止乱码,需要在数据传输的 ...