题目链接: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. 潘石屹的SOHO小报猝死

    东莞时报多媒体数字报刊平台 潘石屹的SOHO小报猝死

  2. 用Scrapy写一个爬虫

    昨天用python谢了一个简单爬虫,抓取页面图片: 但实际用到的爬虫需要处理很多复杂的环境,也需要更加的智能,重复发明轮子的事情不能干, 再说python向来以爬虫作为其擅长的一个领域,想必有许多成熟 ...

  3. html5 app图片预加载

    function Laimgload(){} //图片预加载JS Laimgload.prototype.winHeight = function(){ //计算页面高度 var winHeight ...

  4. AngularJs学习笔记2——四大特性之MVC

    angularJs的四大特性 ①.采用MVC的设计模式 ②.双向数据绑定 ③.依赖注入 ④.模块化设计 现在细说一下MVC的设计模式: MVC: Model(模型)--项目中的数据 View(视图)- ...

  5. tomcat 7配置数据库连接池,使用SQL Server2005实现

    昨 天看了一些网上的tomcat数据库连接池配置的东西,但是一直没配好,主要原因是网上的文章几乎没有针对tomcat 7进行配置的,而且针对SQL SERVER的也不多,今天上午看了官方的文档,花了一 ...

  6. Android BLE开发之Android手机与BLE终端通信

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...

  7. silverlight 双坐标轴

    public void CreateLine(Grid oGrid, string sTitle, string sTableName, bool ifGetSig, string sYUint, s ...

  8. css 文本域textarea显示成label标签

    <html> <head>     <title>textarea显示为label</title> <style type="text/ ...

  9. LeetCode-Divdend two Integers

    题目: Divide two integers without using multiplication, division and mod operator. 思路分析 二分法.将除数不断增倍,而结 ...

  10. linux上安装apache以及httpd.conf基本配置

    1.yum安装apache #yum install httpd -y 2.随系统自启动 #chkconfig httpd on 3.开启apache #service httpd start PS: ...