动态规划练习 3

题目: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;
}
posted @ 2012-08-04 21:08  紫红的泪  阅读(334)  评论(1编辑  收藏  举报