题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1591
该题类似约瑟夫问题,要处理好报数为1的边界情况。
#include <iostream>
using namespace std;
#define MAX 51
int main()
{
freopen("in.txt","r",stdin);
bool ok[MAX],first;
int i,j,k,c,m,n,x,s,time;
time = 1;
first = true;
while(cin >> n >> x)
{
if(!first)
cout << endl;
first = false;
for(i = 1;i <= n;++i)
ok[i] = true;
m = n;
for(i = 0;i < 20;++i)
{
cin >> s;
if(m != x)
{
for(k = 1;k <= n && !ok[k];++k);
c = 1;
if(c == s)//刚开始没考虑s==1的特殊情况导致错误
{
ok[k] = false;
c = 0;
--m;
if(m == x)
continue;
}
for(j = k + 1;j <= n;++j)
{
if(ok[j])
{
++c;
if(c == s)
{
ok[j] = false;
c = 0;
--m;
if(m == x)
break;
}
}
}
}
}
cout << "Selection #" << time << endl;
++time;
for(j = 1;j <= n;++j)
if(ok[j])
cout << j << ' ';
cout << endl;
}
return 0;
}