poj 2081 Recaman's Sequence【hash】
题目意思不难理解就是第m个位置的数是根据第m-1位置的数推出来的如果a[m-1]-m>0,并且a[m-1]-m在前面的序列中没有出现过那么a[m] = a[m-1]-m否则a[m] = a[m-1]+m
另外唯一需要注意的一点就是hash数组开大一点。
//打表
#include <stdio.h>
#include <iostream>
using namespace std;
const int MAXN=500003;
int a[MAXN]={0};
bool hash[10000000]={false};
void prepare()
{
//预处理表
int i;
for(i=1;i<MAXN-1;i++)
{
if (a[i-1]-i>0 && hash[a[i-1]-i]==false)
{
a[i]=a[i-1]-i;
//改变hash值
hash[a[i-1]-i]=true;
}
else
{
a[i]=a[i-1]+i;
//改变hash值
hash[a[i-1]+i]=true;
}
}
}
int main()
{
int k;
prepare();
while(~scanf("%d",&k) && k!=-1)
printf("%d\n",a[k]);
return 0;
}
最后更新:2017-04-03 05:39:41