Codevs 1282 约瑟夫问题

1282 约瑟夫问题

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 
 
题目描述 Description

有编号从1到N的N个小朋友在玩一种出圈的游戏。开始时N个小朋友围成一圈,编号为I+1的小朋友站在编号为I小朋友左边。编号为1的小朋友站在编号为N的小朋友左边。首先编号为1的小朋友开始报数,接着站在左边的小朋友顺序报数,直到数到某个数字M时就出圈。直到只剩下1个小朋友,则游戏完毕。

现在给定N,M,求N个小朋友的出圈顺序。

输入描述 Input Description

唯一的一行包含两个整数N,M。(1<=N,M<=30000)

输出描述 Output Description

唯一的一行包含N个整数,每两个整数中间用空格隔开,第I个整数表示第I个出圈的小朋友的编号。

样例输入 Sample Input

5 3

样例输出 Sample Output

3 1 5 2 4

#include<iostream>
using namespace std;
int i,m,n,current,rest;
int a[30001];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)a[i]=i;
    rest=n;
    current=1;
    while(rest>1){
      current=(current+m-1)%rest;
      if(current==0)current=rest;
      cout<<a[current]<<' ';
      for(i=current;i<=rest-1;i++)a[i]=a[i+1];
      rest--;
    }
    cout<<a[1]<<endl;
    return 0;
}
100分 模拟
原文地址:https://www.cnblogs.com/thmyl/p/7308511.html