[HNUOJ 10045] Joseph‘s Puzzle

Joseph's Puzzle
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 1140, Accepted users: 771
Problem 10045 : No special judgement
Problem description
Persons, whose number order is from 1 to N, hold a password (the straight integer less than 10000) each person and sit around clockwise.
At first we choose a positive integer factor as the count off number M and start to number clockwise from number one .As we number to M the person who numbers M leaves the rank and whose password will be the new number M. And then start to number again from the person who is sit clockwise next to the person who has been out off the rank just before then. Continue like this, until all people are out off the rank completely. Please design a program of leaving order.
 
Input
There have some groups data. First line of each group data is two integers N, M (0<N, M<100),the second line is N positive integer, separately express every person’s password.The last line contins 0,0 ,which means the end of input data.
 
Output
To each group of data, according to the order which leaves outputs their serial number. Between the numeral separates with a blank space. Each group of data monopolize line of outputs.
 
Sample Input
7 20
3 1 7 2 4 8 4
4 3
1 2 3 4
0 0
Sample Output
6 1 4 7 2 3 5
3 2 1 4
Problem Source

HNU 1'st Contest

AC:

 

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring> 
#include<cstdlib>
#include <queue>

struct P
{
    P(int _i = 0, int _pass=0)
    {
        i = _i; pass = _pass;
    }
    int i, pass;
};
std::queue <P> q;

int main()
{
    int n, m, pass;
    P tmp, c;
    while (~scanf("%d%d", &n, &m) && n != 0 && m != 0)
    {
        while (!q.empty()) q.pop();
        for (int i = 0; i < n; ++i)
        {
            scanf("%d", &pass);
            tmp.i = i + 1; tmp.pass = pass;
            q.push(tmp);
        }
        while (!q.empty())
        {
            c.pass = m;
            while (!q.empty() && c.pass != 0) {
                tmp = q.front();
                q.pop();
                --c.pass;
                if (c.pass != 0) q.push(tmp);
                else { c = tmp; printf("%d", c.i); if (!q.empty()) printf(" "); else printf("\n"); };
            }
        }
    }
    return 0;
}

 

上一篇:Optional


下一篇:剑指 Offer 09. 用两个栈实现队列