问题描述如下:
代码 :
#include <stdio.h>
#define N 300
int A[N];
int main()
{
int n,m;
int step,pos; //记录第几步,当前位置
int count; //当前果子数目;
int tag; //记录当前吃掉的果子
int lastPos;
bool flag;
while (scanf("%d%d",&n,&m)==2)
{
for (int i=1;i<=n;++i)
{
A[i]=i;
}
step=1; pos=1;
count=n;
tag=0;
lastPos=0;
flag=true;
while (step<=m)
{
pos +=(step*step*step%5+1); //jump
if (pos>count)
{
if (pos==lastPos)
{
//printf("-1\n");
flag=false;
break;
}
lastPos=pos;
pos=1; //回到起点重新跳
continue;
}else
{
tag=A[pos];
//printf("%d\n",tag);
A[pos]=-1;
for (int j=pos+1;j<=count;++j)
{
A[j-1]=A[j]; //果子下落
}
count--;
}
step++;
}
if (flag)
{
printf("%d\n",tag);
}
else
{
printf("-1\n");
}
}
return 0;
}