约瑟夫环的问题
class Solution {
public:
int LastRemaining_Solution(int n, int m)
{
if(n<1||m<1) return -1;
int *array= new int[n];//用数组模拟环
int i=-1,step=0,count=n;
while(count>0)
{
i++;
if(i>=n) i=0;//模拟环的访问
if(array[i]==-1) continue;//遇到已经访问的小朋友就跳过
step++;//一直遍历小朋友
if(step==m)//找到这位小朋友
{
array[i]=-1;
step=0;
count--;
}
}
return i;
}
};