题目地址:点击打开链接
题意:给你一堆人,标号从2开始依次递增,标号为2的人不用干活,然后往后数2个数,被点到人去干活,然后又重头开始,选到第一个不用干活的人的标号为i,然后往后数i个数,被点到人去干活,就这样循环下去,问第n个不用干活的人的标号是多少?
思路:上来题没看清还以为使用埃拉托斯尼筛法求素数表了,描述太像了,结果打了个素数表就交了,wrong了,后来终于把题看清了,埃拉托色尼筛法是倍数,而这个是往后数,了解题意以后简单模拟一下就过了
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
using namespace std;
const int maxn = 40000;
int select[maxn],nselect;
bool beselect[maxn];
void doselect()
{
int i,j;
nselect = 0;
memset(beselect,true,sizeof(beselect));
for(i=2; i<maxn; i++)
{
if(beselect[i])
{
select[++nselect] = i;
int sum = 0;
for(j=i+1; j<maxn; j++)
{
if(beselect[j])
{
sum++;
if(sum == i)
{
sum = 0;
beselect[j] = false;
}
}
}
}
}
}
int main()
{
int n;
doselect();
while(scanf("%d",&n) && n)
{
printf("%d\n",select[n]);
}
return 0;
}