Recaman's Sequence
Time Limit: 3000MS   Memory Limit: 60000K
Total Submissions: 22566   Accepted: 9697

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

Java AC 代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
int n = 0;
int[] result;
boolean[] marked = new boolean[10000000];
while((n = sc.nextInt()) != -1) {
result = new int[n + 1];
for(int i = 1; i < n + 1; i++) {
int a = result[i - 1] - i;
if(a > 0 && !marked[a]) {
result[i] = a;
marked[a] = true;
}
else {
result[i] = a + 2 * i;
marked[a + 2 * i] = true;
}
}
System.out.println(result[n]);
for(int i = 0; i < 10000000; i++ )
marked[i] = false;
}
}
}

最新文章

  1. LINQ系列:LINQ to SQL Where条件
  2. 利用lambda和Collection.forEach
  3. 在WebApi中实现Cors访问
  4. 笔记9:winfrom的一些知识点(二)
  5. 一步一步理解Paxos算法
  6. 【练习】创建私有的dblink
  7. OSGI框架学习
  8. maven项目在tomcat中运行遇到的问题
  9. 我的VSTO之路(二):VSTO程序基本知识
  10. jQuery选择器(ID选择器)第一节
  11. Android开发之漫漫长途 Ⅰ——Android系统的创世之初以及Activity的生命周期
  12. vs2015 c# winfrom应用程序打包成64位
  13. mysql 案例 ~ 函数汇总
  14. java学习--equals
  15. 使用kolla安装的openstack mariadb为集群所有节点无法启动
  16. mac上使用zsh配置环境变量
  17. PHP查找中文字符的解决方案
  18. 使用jar命令打jar/war包、创建可执行jar包、运行jar包、及批处理脚本编写
  19. css常用标签及属性
  20. fullpage.js全屏滚动插件使用方法

热门文章

  1. 自定义组合控件SettingItemView的简单实现
  2. Android中IntentService与Service
  3. [iOS]UIWebView返回和NSURLErrorDomain-999
  4. C# 跨线程更新 UI
  5. sh脚本实战
  6. Jquery(DOM和选择器)
  7. 2-0 虚拟机与Linux系统安装
  8. EntityFramework数据迁移(笔记)
  9. 吉首大学2019年程序设计竞赛-F 天花乱坠
  10. 【转帖】超能课堂(188) WiFi 6凭什么可以如此“六”?