码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2081 Recaman's Sequence

时间:2015-03-08 00:02:47      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

比较简单,加一个B数组判重即可

 

Recaman‘s Sequence
Time Limit: 3000MS   Memory Limit: 60000K
Total Submissions: 21743   Accepted: 9287

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.

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

Source

 
技术分享
 1 //oimonster
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<iostream>
 5 using namespace std;
 6 int a[500001],b[10000000]={0};
 7 int main(){
 8     int i,j,n,m;
 9     a[0]=0;
10     b[0]=1;
11     for(i=1;i<=500000;i++){
12         m=a[i-1]-i;
13         if((m<=0)||(b[m]==1)){
14             a[i]=a[i-1]+i;
15             b[a[i]]=1;
16         }
17         else{
18             a[i]=m;
19             b[m]=1;
20         }
21     }
22     scanf("%d",&n);
23     while(n!=-1){
24         printf("%d\n",a[n]);
25         scanf("%d",&n);
26     }
27     return 0;
28 }
View Code

 

POJ 2081 Recaman's Sequence

标签:

原文地址:http://www.cnblogs.com/oimonster/p/4321143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!