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. (转) Linux命令详解-date

    Linux命令详解-date 原文:https://www.cnblogs.com/Dodge/p/4278292.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到 ...

  2. yum指令之修复

    折腾着搞 openvpn 网站服务器 yum指令 出了点问题 ------------------------------------------------------------ [root@cl ...

  3. Memcached分布式原理

    http://younglibin.iteye.com/blog/2043761 浅显易懂,值得一读

  4. java内存分配(堆、栈、常量池)

    Java内存分配: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,以及成员方法中的局部变量 ◆堆:存放对象本身(成员变量+成员方法的引用) ◆静态域:存放在对象中用static ...

  5. dubbo注解

    如果还不了解Dubbo是什么或者不知道怎么搭建的可以先看一下我的上一篇文章. 首先我先来讲下提供者(也就是服务端)的配置,先上配置文件代码: <?xml version="1.0&qu ...

  6. Dubbo 使用rest协议发布http服务

    演示用GitHub地址:https://github.com/suyin58/dubbo-rest-example 1       Dubbo_rest介绍 Dubbo自2.6.0版本后,合并了dub ...

  7. C#调用C++接口返回字符串的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 现在有这样一种情景,假如C#调用C++接口需要返回一个字符串.因为字符串是不定长的,因此传递一个定长的字符串进去是不合 ...

  8. cpp 计算程序运行时间的两种方法

    1. #include <time.h> time_t begin_t = clock(); // to do time_t finish_t = clock(); cout<< ...

  9. iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

    iOS Programming Autorotation, Popover Controllers, and Modal View Controllers  自动旋转,Popover 控制器,Moda ...

  10. IT届常用单词读法纠正

    Bootstrap    ['bʊt'stræp] Java  ['dʒɑːvə] Node           [nod] @  [æt; ət] Common      ['kɑmən] Java ...