POJ 2081 Recaman's Sequence

用一个数组储存数列中的数,一个数组储存出现过的数即可..

#include <cstdio>
using namespace std;
int a[500001];
int pos[5000001]//开的有点大。。反正内存够用。。;
int main(){
	for(int i=0;i<=5000000;i++){
		pos[i]=0; 
	}
	a[0]=0;
	pos[0]=1;
	for(int i=1;i<=500000;i++){
		int t=a[i-1]-i;
		if(t>0&&pos[t]==0)a[i]=t;
		else a[i]=a[i-1]+i;
		pos[a[i]]=1;
	}
	int n;
	while(scanf("%d",&n)){
		if(n==-1)break;
		printf("%d\n",a[n]);
	}
	return 0;


你可能感兴趣的:(POJ 2081 Recaman's Sequence)