程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> uva-133 The Dole Queue

uva-133 The Dole Queue

編輯:C++入門知識

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.

這個題的意思是給你一個長度為N的循環隊列,一個人從1號開始逆時針開始數數,第K個出列,一個人從第N個人開始順時針數數,第M個出列,選到的兩個人要同時出列(以不影響另一個人數數),選到同一個人只出隊一次。

解題思想:用數組模擬,arr[i]等於0時表示第I個人出列。

 

#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>

using namespace std;
int main()
{

    int n,k,m,size;
    int arr[20];
    while(cin>>n>>k>>m)
    {
        if(n==0&&k==0&&m==0)break;
        int p=0,q=n-1;
        size=n;
        for(int i=0; i<n; i++)
            arr[i]=i+1;
        while(size>0)
        {
            for(int i=1; i<k; i++)
            {
                p=(p+1)%n;
                if(!arr[p])i--;
            }
            for(int i=1; i<m; i++)
            {
                q=(q-1+n)%n;
                if(!arr[q])i--;
            }
            if(arr[p])
            {
                printf("%3d",arr[p]);
                arr[p]=0;
                size--;
            }
            if(arr[q])
            {
                printf("%3d",arr[q]);
                arr[q]=0;
                size--;
            }
            for(int i=0; i<n; i++)
            {
                p=(p+1)%n;
                if(arr[p])break;
            }
            for(int i=0; i<n; i++)
            {
                q=(q-1+n)%n;
                if(arr[q])break;
            }
            if(size)printf(",");
            else printf("\n");
        }
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved