zoukankan      html  css  js  c++  java
  • POJ 2081

    Recaman's Sequence
    Time Limit: 3000MS   Memory Limit: 60000K
    Total Submissions: 18575   Accepted: 7751

    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
    
    #include <stdio.h>
    #include <string.h>
    const int N=500010;
    int ch[N];
    bool vis[N*10];//状态数组一定要开得比数字数组大,否则测试时会RE 
    void init()
    {
        int i,j;
        memset(vis,false,sizeof(vis));
        memset(ch,0,sizeof(ch));
        vis[0]=vis[1]=vis[3]=true;
        ch[0]=0;ch[1]=1;
        for(i=2;i<N;i++)
        {
            ch[i]=ch[i-1]-i;
            if(ch[i]<1||vis[ch[i]])
                ch[i]=ch[i-1]+i;
            vis[ch[i]]=true;
        }
        return ;           
    }
    int main()
    {
        int i,j,k;
        init();
        while(scanf("%d",&k),k!=-1)
            printf("%d\n",ch[k]);
        return 0;
    }
        
        
        
    
  • 相关阅读:
    第01组 Alpha冲刺 (1/6)
    第01组(17) 需求分析报告
    第01组(17) 团队展示
    结对编程作业
    05 RDD编程
    第九次作业
    HDFS
    Hadoop安装 与 HDFS体系结构
    第三次作业
    作业2
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2619475.html
Copyright © 2011-2022 走看看