CF 792 B. Counting-out Rhyme(水题)

7 篇文章 0 订阅

Counting-out Rhyme

n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.

For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.

You have to write a program which prints the number of the child to be eliminated on every step.

Input
The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1).

The next line contains k integer numbers a1, a2, …, ak (1 ≤ ai ≤ 109).

Output
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.

Examples
input
7 5
10 4 11 4 1
output
4 2 5 6 1
input
3 2
2 5
output
3 2
Note
Let’s consider first example:

In the first step child 4 is eliminated, child 5 becomes the leader.
In the second step child 2 is eliminated, child 3 becomes the leader.
In the third step child 5 is eliminated, child 6 becomes the leader.
In the fourth step child 6 is eliminated, child 7 becomes the leader.
In the final step child 1 is eliminated, child 3 becomes the leader.

题意:n个人围成一圈,从第一个人开始每一轮从后一个往下数ai个人,最后一个出圈,然后下一个人再接着进行下一轮,输出每次出圈的人的编号。
解析:假设那一轮有x个人,显然每次只需往下数ai%x个人就可以了,我们用一个vis数组记录这个编号的人有没有出去,从报数的那个人开始依次往下遍历,如果出列了就跳过,直到报完。

代码:(写得很丑,见谅)

#include<iostream>
#include<cstring>
#include<math.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200001
#define INF 0x3f3f3f3f
#include<map>
#include<string>
#include<queue>
#include<vector>
#include<set>
#define mod 9901
double pi=acos(-1.0);
typedef long long ll;
using namespace std;
int n,k,pos;
ll a;
int vis[105];
int main(){
    memset(vis,0,sizeof(vis));//vis数组
    cin>>n>>k;
    int num=n;//圈中的人数
    pos=1;//记录报数的人的位置
    for(int i=0;i<k;i++)
    {
        cin>>a;
        int ans=a%num;//应该往下数的人数
        if(ans==0)//为0的时候要特判,在这儿WA了好几发
        {
            if(pos%n==0) cout<<n<<" ";
            else cout<<(pos+n)%n<<" ";
            vis[pos%n]=1;
            num--;
            while(vis[pos%n]) //找到下一个报数的人
            {
                pos++;
                pos%=n;
            }
            continue;
        }
        pos++;
        while(vis[pos%n]) 
        {
            pos++;
            pos%=n;
        }
        while(ans>0)
        {
            if(!vis[pos%n])
            {
                ans--;
                if(ans==0) 
                {
                    vis[pos%n]=1;
                    if(pos%n!=0)
                    cout<<(pos)%n<<" ";
                    else cout<<n<<" ";
                }
                pos++;
                pos%=n;
            }
            else 
            {
                pos++;
                pos%=n;
            }
        }
        while(vis[pos%n]) //找到下一个报数的人
        {
            pos++;
            pos%=n;
        }
        num--;//圈中的人出去,人数减少
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值