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. PHP函数生成随机数

    通常情况下,当我们要生成一个随机字符串时,总是先创建一个字符池,然后用一个循环和mt_rand()或rand()生成php随机数,从字符池中随机选取字符,最后拼凑出需要的长度,代码如下: <?p ...

  2. Mybatis find_in_set 子查询,替代 in

    1. Mapper文件 2.dao层 3.生成Sql

  3. storm中的topology-worker-executor-task

    调度角色 调度方法 自定义调度 1 调度角色   任务角色结构 上图是JStorm中一个topology对应的任务执行结构,其中worker是进程,executor对应于线程,task对应着spout ...

  4. 在CentOS7上源码安装php7--Install php7 from source on CentOS7

    首先下载php源码包并解压: # wget http://cn2.php.net/get/php-7.0.9.tar.gz/from/this/mirror # .tar.gz # cd php- 然 ...

  5. css position 定位模式

     定位 定位模式: static relative absolute fixed 边偏移 :top bottom left right 一般的定位必须要有定位模式以及边偏移 static 静态定位 默 ...

  6. gulp运行步骤

    一.运行→输入cmd→跳出命令窗口二.cd D: 敲回车进入D盘,cd www 进入项目路径 cd mygulp三.执行 cnpm install gulp --save-dev 命令 (初始化安装g ...

  7. 【Unity3D】资源对象、预设、查找对象、组合模式等知识点

    1.解释对象与资源区别于联系,根据官方案例,分别总结资源和对象组织的规则/规律.    下载并查看了Adam和Survival Shooter tutorial两个官方资源案例,这些案例作为资源,可以 ...

  8. c++中三种继承方式的区别

    public公有继承 protected保护继承 private私有继承 我们知道类的private和protected成员,在类外是不可以使用的.只有public成员可以在类外直接使用. 公有继承时 ...

  9. Java transient关键字使用

    1. transient的作用及其使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的 ...

  10. git如何强制用远程分支更新本地

    git本地即使有修改如何强制更新: 本地有修改和提交,如何强制用远程的库更新本地.我尝试过用git pull -f,总是提示 You have not concluded your merge. (M ...