zoukankan      html  css  js  c++  java
  • UVa133

    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

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

    where tex2html_wrap_inline50 represents a space.

    题意:

           有N个人围成一圈、给其中任意一人编号为1,顺时针依次从1到N给剩余的每个人编号。N号应该就在1号的旁边。现在每回合不断选走一个或者两个人,知道没有人剩下,规则是这样的:首先从1号开始每次顺时针地数k个人(包括1号)并选定第k个人,然后从N号开始逆时针地数m个人(包括N号)并选定第m个人,如果前面选定的那个人与后面选定的那个人重合,则选走那一个人,否则选走分别选定的两个人。每次都是顺时针数k个人和逆时针数m个人,只不过都从分别选定的那个人的下一个人开始数起。按顺序输出每次被选走的人。

    输入:

           多组数据,每组给出N,k,m。以0,0,0作为输入的结束。

    输出:

           每组数据按顺序输出每次出圈人的编号,次与次之间有逗号间隔,整数都要占3个空格的位置并右顶格。

    分析:

           模拟题。每次遍历整个环形数列,如果已经选走则将它置为零。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 const int MAX_N = 20;
     8 int n,k,m;
     9 int arr[MAX_N + 1];
    10 int main(){
    11     while(scanf("%d%d%d",&n,&k,&m) == 3){
    12         if(!n && !k && !m) break;
    13         int size = n;
    14         int p = 0,q = n - 1;
    15         for(int i = 0 ; i < n ; i++) arr[i] = i + 1;
    16         while(size > 0){
    17             for(int i = 1 ; i <= k - 1 ; i++){
    18                 p = (p + 1) % n;
    19                 if(!arr[p]) i--;
    20             }
    21             for(int j = 1 ; j <= m - 1 ; j++){
    22                 q = (q - 1 + n) % n;
    23                 if(!arr[q]) j--;
    24             }
    25             printf("%3d",arr[p]);
    26             arr[p] = 0;
    27             size--;
    28             if(arr[q]){
    29                 printf("%3d",arr[q]);
    30                 arr[q] = 0;
    31                 size--;
    32             }
    33             for(int i = 0 ; i < n ; i++){
    34                 p = (p + 1) % n;
    35                 if(arr[p]) break;
    36             }
    37             for(int j = 0 ; j < n ; j++){
    38                 q = (q - 1 + n) % n;
    39                 if(arr[q]) break;
    40             }
    41             printf("%c",size == 0 ? '
    ' : ',');
    42         }
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    Mono.Ceil 无法保存Silverlight 程序集
    MyFramework框架搭建(二)DAL层整体通信
    MyFramework框架搭建(一)DAL层
    使用 tke-autoscaling-placeholder 实现秒级弹性伸缩
    被集群节点负载不均所困扰?TKE 重磅推出全链路调度解决方案
    一文读懂 TKE 及 Kubernetes 访问权限控制
    一文读懂 Kubernetes APIServer 原理
    K8s 平台可以如何处理 Pod 预授权问题
    【从0到1学习边缘容器系列-4】弱网环境利器之分布式节点状态判定机制
    一文读懂 SuperEdge 边缘容器架构与原理
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5778354.html
Copyright © 2011-2022 走看看