刚刚交了一道题目,总结一下

# include 
long int recaman[500001];
int used[5000000] = {0};
	// if an integer is not used, the mark is 0 

int main(void)
{
	long int i, k;
	long int m;

	for (i = 0; i < 500001; i++)
	{
		if (i - 1 < 0)
		{
			recaman[i] = 0;
		}
		else
		{
			m = recaman[i-1] - i;
			if (m > 0 && used[m] == 0)
				recaman[i] = m;
			else
				recaman[i] = m + i + i;
		}
		used[recaman[i]] = 1;
	}
	while(scanf("%ld", &k) != EOF && k >= 0)
	{
			printf("%ld\n", recaman[k]);
	}

	return 0;
}

Recaman's SequenceTime Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 1183, Accepted users: 1023 Problem 10010 : No special judgement

Problem description

The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence, otherwise am = am−1 + m.
The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ...
Given k, your task is to calculate ak.

The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ...
Given k, your task is to calculate a k .

Input

The input consists of several test cases. Each line of the input contains an integer k where 0 <= k <= 500000. The last line contains an integer −1, which should not be processed.


Output

For each k given in the input, print one line containing ak to the output.


Sample Input
7
10000
-1
Sample Output
20
18658


if (m > 0 && used[m] == 0)


注意一点,C语言中有一个特点,就是逻辑与运算,千万不要把上面这条语句&&两边的调换过来,否则数组访问会出错!!



while(scanf("%ld", &k) != EOF && k >= 0)
 
  


另外还有一点,写代码时候千万要认真,刚才就是在上面这个语句后面加了分号,,,,,我结果后来才发现。
 
  

你可能感兴趣的:(刚刚交了一道题目,总结一下)