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

13874119 133 The Dole Queue Accepted C++ 0.009 2014-07-13 02:44:49

 The Dole Queue 

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 counter-clockwise 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

Write a program that will successively read in (in that order) the three numbers (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 counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).

Sample input

10 4 3
0 0 0

Sample output

4 8, 9 5, 3 1, 2 6, 10, 7

where represents a space.


解题思路:一道非常类似约瑟夫问题的题目。http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1197
     (关于约瑟夫问题,可以翻阅《具体数学》第一章引例。)

     直接数组模拟就好,没特殊机巧。白书上标程的写法更加精炼,要多学学优化自身的代码!

 #include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int maxn = ;
int n, m, k, peo[maxn]; int solve_z(int cur_first, int step_time) {
int ans = cur_first;
while (step_time --) {
ans ++;
if (ans > n) ans = ;
while (peo[ans] == -) {
ans ++;
if (ans > n) ans = ;
}
}
return ans;
} int solve_f(int cur_first, int step_time) {
int ans = cur_first;
while (step_time --) {
ans --;
if (ans == ) ans = n;
while (peo[ans] == -) {
ans --;
if (!ans) ans = n;
}
}
return ans;
} int main() {
while (cin >> n >> k >> m) {
if (n + k + m == ) break;
for (int i = ; i <= n; i ++) {
peo[i] = i;
} int left_peo = n;
int cur1 = n, cur2 = ;
while (left_peo != ) {
cur1 = solve_z(cur1, k);
//cout << cur1 << endl;
cur2 = solve_f(cur2, m);
//cout << cur2 << endl;
//system("pause");
printf("%3d", cur1); left_peo --;
if (cur2 != cur1) {
printf("%3d", cur2);
left_peo --;
}
//cout << ",";
peo[cur1] = peo[cur2] = -;
if(left_peo) cout << ",";
}
cout << endl;
} return ;
}

UVa133.The Dole Queue的更多相关文章

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

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

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

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

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

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

  4. UVA 133 The Dole Queue

    The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...

  5. The Dole Queue

    The Dole Queue Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid ...

  6. 水题:UVa133-The Dole Queue

    The Dole Queue Time limit 3000 ms Description In a serious attempt to downsize (reduce) the dole que ...

  7. The Dole Queue UVA - 133

     In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros ...

  8. uva - 133 The Dole Queue(成环状态下的循环走步方法)

    类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...

  9. UVA 133 The Dole Queue(报数问题)

    题意:一个长度为N的循环队列,一个人从1号开始逆时针开始数数,第K个出列,一个人从第N个人开始顺时针数数,第M个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...

随机推荐

  1. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  2. Windows下命令行直接编译程序

    D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...

  3. JavaScript总结(二) 系统分析

    学习Javascript的时候.遇到了一个问题. 看了牛腩的视频,感觉讲的太简单和基础,像认识一位新朋友.但仅有一面之缘就结束了. 再看姜浩的视频.又看得迷迷糊糊,让我找到了当初看两位台湾专家讲VB. ...

  4. [华为机试练习题]55.最大公约数 &amp; 多个数的最大公约数

    题目 描写叙述: 输入2个数字,最后输出2个数字的最大公约数 题目类别: 位运算 难度: 0基础 执行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 2个整数 输出: 输出数字1和 ...

  5. [ASP.NET] 檔案讀寫權限問題

    今天遇到一個問題,環境如下: IIS Server: Server 2008 R2 沒加域 File Server: Server 2003 加域 當我的Web程序需要把位於File Server的一 ...

  6. 报错要跟到底就很更快更准确的发现问题所在一直in进去(其实都知道的哈)

    问题-查看详细信息-innerexception-innerexception

  7. Asp.net实现在线人数统计功能代码实例

    application最经典的一个方法:统计在线人数,这需要借助于我们的全局应用程序类来对登录的用户信息进行统计: 以下是代码片段:    void application_start(object ...

  8. app打包,发布(同步发生冲突)

    1:打包步骤: 1:桌面建立一个文件夹,名字叫keystore 2:点击build下面的 ,如下:     3:会出现如下界面: 4:下一步: 5:如果有keystore,请点击 choose exi ...

  9. Geodatabase - 创建要素类.

    在NET中,会遇到以“_2”结尾的属性,这些属性是可写的. 以下代码在已有工作空间下,创建一个新的点要素类: //例如,personalDBPath=@"G:\doc\gis\1.400\d ...

  10. JavaScript优化参考

    最近在为管理系统的网站做点优化,压缩都用了工具,就没太多可以讨论的. 主要还是代码上的精简和优化.稍微整理一下,顺便做点测试. 这里先贴上项目中用来替代iFrame的Ajax处理的局部代码,本人比较讨 ...