【FOJ】Problem 1304 Recaman's Sequence

Problem 1304 Recaman’s Sequence.

The meaning of problems

  • A (0) = 0
    m> 0, A (m) = A (m-. 1) -m, (A (m) is a positive number not in the sequence), or a (m) = a (m -1) + m.
  • Input:
    K, 0 <= K <= 500000
    -1, the input end
  • Output:
    A (K)

Thinking

Two arrays, A [] is used to store the sequence, In Flag [i] for i is marked in the sequence, when set to 1, is not set to 0

Code

#include<cstdio>
using namespace std;

int a[500010] = {0};
bool flag[10000000] = {false};

int main(){
	int k, x;
	flag[0] = true;
	for(int i=1; i<=500000; i++){
		x = a[i-1] - i;
		if(x>0 && flag[x]==false)
			a[i] = x;
		else
			a[i] = x + 2*i;
		flag[a[i]] = true;
	}
	scanf("%d", &k);
	while(k!=-1){
		printf("%d\n", a[k]);
		scanf("%d", &k);
	}
	return 0;
}
Published 28 original articles · won praise 0 · Views 313

Guess you like

Origin blog.csdn.net/qq_44531167/article/details/105351177