MRECAMAN - Recaman’s Sequence

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<cstdio>
#include<set>
using namespace std;

#define K 500000
int a[K+1], k;

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	set<int> my;
	for(int i = 1; i <= K; ++i) {
		a[i] = a[i-1] - i;
		if(a[i] <= 0 || a[i] > 0 && my.find(a[i]) != my.end())
			a[i] = a[i-1] + i;
		my.insert(a[i]);
	}
	while(scanf("%d", &k) != EOF && k != -1)
		printf("%d\n", a[k]);
	return 0;
}

Download