有m个猴子,每次报一个数,最后一个留下的为大王。
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
struct one
{
int num;
one *next;
};
int m,n;
int main()
{
int i,j,k,t;
cin>>t;
one *N,*cur,*you;
while (t--)
{
cin>>m>>n;
N=NULL;
for (i=0;i<m;i++)
{
cur=new one;
cur->next=NULL;
cur->num=i+1;
if (N==NULL)
{
N=cur;
you=N;
}
else
{
N->next=cur;
N=N->next;
}
}
N->next=you;
N=you;
if (n==1)
cout<<"The big result is : "<<m<<endl;
else
{
k=2;
while (true)
{
if (N->next==N)
{
cout<<N->num<<endl;
break;
}
if (k==n)
{
you=N->next;
N->next=N->next->next;
delete you;
N=N->next;
k=2;
continue;
}
N=N->next;
k++;
}
}
}
}