有17个人围成一圈

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:51:23
有17个人围成一圈,从0号的人开始报数,凡报到3的倍数的人离开圈子,然后再数下去。直到最后只剩下一个人为止。问此人原来的位置是多少号。

也许这个题目网上很多 但是我希望大家给我一个非常详细的答案,源代码上请打上注释 还有最好用比较容易的编程法 随便粘贴的就免了

输出是8

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
const int NUM = 17;
// 数组
int a[NUM];
// 序号
int i=0;
// 剩下的个数
int numleft = NUM;
// 当前报数
int total=1;
// 当前人的指针
int *p = a;
//初始化
for(i=0;i<NUM;i++)
a[i] = 1+i;

while(numleft > 1 )
{
if(total%3 == 0)
{
int j=0;
while(a+numleft != (p+j))
{
*(p+j) = *(p+j+1);
j++;
}
numleft--;
}

if(a+numleft == p)
p = a;
else
p++;
total++;
}

cout<< a[0]<<endl;

}

解:
程序在VC中运行通过
Seq_____kicked__________count

1.______man[0]__________0
2.______man[3]__________3
3.______man[6]__________6
4.______man[9]_____