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


解题心得:

  1. 紫书上的例题,很简单可以直接模拟整个过程就行了,但是要注意输出的问题(”%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的更多相关文章

  1. UVa133.The Dole Queue

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

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

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

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

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

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

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

  5. CF451C Predict Outcome of the Game 水题

    Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...

  6. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

  7. CF451A Game With Sticks 水题

    Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second mem ...

  8. hdu1240 bfs 水题

    原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...

  9. CCF 201612-1 最大波动 (水题)

    问题描述 小明正在利用股票的波动程度来研究股票.小明拿到了一只股票每天收盘时的价格,他想知道,这只股票连续几天的最大波动值是多少,即在这几天中某天收盘价格与前一天收盘价格之差的绝对值最大是多少. 输入 ...

随机推荐

  1. GIT主要用到的命令

    git add . //添加到暂存盘 git commit -m ‘备注’//提交到本地仓库 git push //提交到远程仓库 fetch更新本地仓库两种方式: //方法一 $ git fetch ...

  2. AI入门丨开源学习资源推荐

    现在AI大热,网上的资源也非常多,让人眼花缭乱.非科班的我,经过半年的摸索,也算马马虎虎入了坑.下面整理了我认为不错的学习资源,大部分我都看过,以分享给更多的人.我会不断保持更新,也欢迎大家补充. P ...

  3. Typora--Draw Diagrams With Markdown

    Typora Typora supports some Markdown extension for diagrams, you could enable this feature from pref ...

  4. CSS属性、伪类选择器,CSS3选择器

    CSS1时IE6是部分支持,伟大的IE6!CSS2时IE6部分支持,伟大的IE6依旧是部分支持!CCS3盛行CSS4也已经提上日程的现在,IE6完全不支持.IE6你该走了,我们会永远记住你的功绩的!I ...

  5. 右侧导航栏(动态添加数据到list)

    页面样式 <style> .scroll { position: fixed; right: 5%; top: 5em; background: #ccc; display: none; ...

  6. 【踩坑】Nginx上配置ssl证书实现https访问

    昨天开始为域名挂上ssl证书,使得可以以https去访问服务器.按照网上所介绍的配置Nginx,然而一直访问不了网站. 第二天排查了一早上,发现不单要配置Nginx,阿里云上安全组要开启443端口,并 ...

  7. dubbo rest返回值异常Incompatible types: declared root type

    2018-08-28 17:26:02,208 [http-bio-9090-exec-1][][][][][] ERROR com.wjs.member.plugin.intercepter.Ser ...

  8. sublime text 快捷键新建.vue

    第一步:添加模板: 模板写法如下: <template> </template> <script type="ecmascript-6"> &l ...

  9. html5 新增表单控件和表单属性

    新的输入型控件 email:电子邮箱文本框,跟普通的没什么区别 当输入不是邮箱的时候,验证通不过 移动端的键盘会有变化         tel:电话号码 一般用于手机端,是一个键盘切换 url:网页的 ...

  10. 数据库操作----找了MySQL和SQL Sever两个的基础语句

    这是MySQL的基本操作: 1 登入数据库:mysql -uroot -p+密码 (SQL Sever登入: osql -U 用户名 -P 密码) 显示已存在的数据库:show databases; ...