前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-2081 Recaman's Sequence

POJ-2081 Recaman's Sequence

作者头像
ShenduCC
发布2018-04-25 17:38:42
4330
发布2018-04-25 17:38:42
举报
文章被收录于专栏:算法修养

Recaman’s Sequence Time Limit: 3000MS Memory Limit: 60000K Total Submissions: 22392 Accepted: 9614 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

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;
#define MAX 500000
long long int a[MAX+5];
bool tag[10000000];
void fun()
{
    a[0]=0;
    tag[0]=1;
    for(int i=1;i<=MAX;i++)
    {
        a[i]=a[i-1]-i;
        if(a[i]<0||tag[a[i]]==1)
            a[i]=a[i-1]+i;
        tag[a[i]]=1;
    }
}
int main()
{
    int n;
    memset(tag,0,sizeof(tag));
    fun();
    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1)
            break;
        printf("%d\n",a[n]);

    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-01-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档