题目:Recaman's Sequence (POJ 2081)
链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=2081
#include <iostream>
#include <memory.h>
using namespace std;
bool exist[9999999];
int recaman[500001];
int main(int argc, char **argv)
{
int k;
recaman[0] = 0;
memset(exist, 0, sizeof(exist));
for (int i = 1; i <= 500000; ++i)
{
if (recaman[i - 1] - i > 0 && !exist[recaman[i - 1] - i])
{
const int ak = recaman[i - 1] - i;
recaman[i] = ak;
exist[ak] = true;
}
else
{
const int ak = recaman[i - 1] + i;
recaman[i] = ak;
exist[ak] = true;
}
}
while (true)
{
cin >> k;
if (k == -1)
{
break;
}
cout << recaman[k] << endl;
}
return 0;
}