Submit: 41 Solved: 33
[ Submit][ Status][ Web Board]
Description
早春星期天的某个早晨,大风呼呼地刮。一个宿舍n个人,谁也不想起床买饭去。他们定了一个规矩,想找出买饭的人。规则如下: n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人继续,剩下的人转圈继续数,最后剩下的那个人去为大家买饭去。 请输出最后这个倒霉蛋的编号。
Input
输入n和m值。
Output
输出倒霉蛋的编号。
Sample Input
5 3
Sample Output
4
HINT
样例求解过程
第一轮:3继续睡
第二轮:1继续睡
第三轮:5继续睡
第四轮:2继续睡
4号买饭去
Source
代码:
#include<iostream>
using namespace std;
int main()
{
int i,t;
int n,m;
int s=0;
int x;
int a[99];
cin>>n>>m;
for(i=1;i<=n;i++)
a[i]=1;
i=1;
x=0;
while(s<n-1)
{
if(a[i]==1)
x++;
if(x==m)
{
a[i]=0;
s++;
}
if(x==m)x=0;
if(s==n-1)break;
i++;
if(i==n+1)
i=1;
}
for(i=1;i<=n;i++)
if(a[i]==1)cout<<i;
return 0;
}