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. sql-monitore 的bug 。

    http://www.mamicode.com/info-detail-1659243.html 存储过程无法做 sql -monitor , 而存储过程跑的sql (只能通过awr 报告来看sql_ ...

  2. 在docker上centos7 编译安装php7

    docker镜像来自daocloud.io/library/centos 首先下载libmcrypt库并make && make install yum -y install gcc ...

  3. Promise 对象与Generator 函数

    异步编程的方法,大概有下面四种: 回调函数 事件监听 发布/订阅 Promise 对象 传统的编程语言,早有异步编程的解决方案(其实是多任务的解决方案).其中有一种叫做"协程"(c ...

  4. java项目定时任务实现

    首先配置spring-context.xml文件 在xmlns 下加如下代码 xmlns:task="http://www.springframework.org/schema/task&q ...

  5. 最具士兵突击实战类型的JavaScript

    JavaScript实战一书的基础知识部分帮助读者快速踏入JavaScript领域之门,jQuery部分帮助读者随心所欲地去工作,HTML5部分帮读者搭上时代的班车,Node.JS则可以让读者屹立在技 ...

  6. freebsd问题

    http://community.spiceworks.com/topic/91708-server-freezes

  7. PHP针对二维数组无限遍历变形研究

    一.需要变形的二维数组 $arr = Array( Array ( 'material_id' => 1, 'material_name' => '铜板纸', 'parent_id' =& ...

  8. HDU Rabbit and Grass 兔子和草 (Nim博弈)

    思路:简单Nim博弈,只需要将所给的数字全部进行异或,结果为0,则先手必败.否则必胜. #include <iostream> using namespace std; int main( ...

  9. vijos 1320 清点人数

    背景 NK中学组织同学们去五云山寨参加社会实践活动,按惯例要乘坐火车去.由于NK中学的学生很多,在火车开之前必须清点好人数. 描述 初始时,火车上没有学生:当同学们开始上火车时,年级主任从第一节车厢出 ...

  10. Codeforces Round #319 (Div. 2) B Modulo Sum (dp,鸽巢)

    直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为前缀sum[i]取余数,i = 0,1,2,3...,m,有m+1个余数,m的余数只有m种必然有两个相同. #include< ...