题目描述
猴子选大王,有N只猴子,从1~N进行编号。它们按照编号的顺时针方向,排成一个圆圈,然后从第一只猴子开始报数。第一只猴子报1,以后每只猴子报的数字都是它前面猴子所报数字加1。如果一只猴子报的数字是M,则该猴子出列,下一只猴子重新从1开始报数。剩下的猴子继续排成一个圆圈报数,直到全部的猴子都出列为止。最后一个出列的猴子胜出。
输入格式
The first line is an integer t, indicating the number of test cases. Then there are t lines and each line contains two positive integer N(0<N<=100) and M(0<M<=100).
输出格式
For each test case, print out the number of the Monkey King.
样例输入
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> 将样例输入复制到剪贴板
2
5 2
4 3
样例输出
3
1
--------------------------我不是分割线-------------------------------------------------------------- 分析:开始时并没有考虑太多,就选择了用struct的数组来简单实现,不过时间上耗费略大
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; struct data
{
int count;
bool is;
}; int cmp(data a[],int n){
int count1=,out;
for (int i = ; i < n; ++i)
{
if (a[i].is==)
{
++count1;
out=a[i].count;
}
}
if (count1==)
{
return out;
}
else{
return ;
}
}
int main(int argc, char const *argv[])
{
int t;
cin>>t;
for (int i = ; i < t; ++i){
int m,n;
cin>>m>>n;
data monkey[m];
for (int i = ; i <m; ++i)
{
monkey[i].count=i+;
monkey[i].is=;
}
int ji=;int count2=;int i=;
int isf=cmp(monkey,m);
while(!isf){
if (monkey[i].is==)
{
++count2;
if (count2==n)
{
monkey[i].is=;
count2=;
}
}
++i;
if(i==m){
i=;/*实现循环,保证由count2来决定循环撤出的有效性*/
}
isf=cmp(monkey,m);
}
cout<<isf<<endl;
}
return ;
}