刚刚交了一道题目,总结一下
来源:互联网 发布:vmware装ubuntu 编辑:程序博客网 时间:2024/04/28 14:51
# include <stdio.h>
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;elserecaman[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:65536KBTotal 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.
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
710000-1Sample Output
2018658
if (m > 0 && used[m] == 0)
注意一点,C语言中有一个特点,就是逻辑与运算,千万不要把上面这条语句&&两边的调换过来,否则数组访问会出错!!
while(scanf("%ld", &k) != EOF && k >= 0)另外还有一点,写代码时候千万要认真,刚才就是在上面这个语句后面加了分号,,,,,我结果后来才发现。
- 刚刚交了一道题目,总结一下
- 我刚刚做了一道题目,我感觉自己好菜啊
- 呵呵,刚刚水了一道题目。简单题就要水!PKU2895,精简到70行代码!
- 我刚刚写了一个map的题目,还用到了#include<string>的头文件,下面我来总结一下 map<类型>变量 q q.find(找字符串) q.end()是否到达末尾
- 刚刚玩弄了一下usb 6009
- 蛙蛙推荐:我自己刚刚填写了一下那天的应聘测试题目,大家多多指教
- 轻松一下,做一道题目吧
- 又做出一道题目了~
- 今天刚刚认真区别了一下两个文件读写方式
- 半年了,总结一下
- 一道做了一天的题目!
- 最近做了一道恶心的题目
- 一道线代题目的总结
- 刚刚激活,使用一下。
- 刚刚开通,测试一下
- 刚刚有水了一道,用递归实现进制转换
- 总结了一下数据库连接池
- 你可以总结一下了
- bmp文件格式详解
- 钟翔平:坚持走手机浏览器架构创新之路
- 用iTunes搞定iPhone自定义铃声,终极版!!
- iOS NSString 连接方式
- springmvc 删除数据时报错:illegally attempted to associate a proxy with two open Sessions
- 刚刚交了一道题目,总结一下
- JSTL-核心标签库
- Hibernate逆向生成class和hbm映射
- 怎么获取Powerpoint动画
- .NET(用户自定义控件实现权限控制)
- IMPDP导入数据提示'insufficient privileges'
- 正则表达式
- Math.random()
- 不同的tomact实现sesson复制,共享session