| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 445 人关注过本帖
标题:为什么编译通过了却运行不了?
只看楼主 加入收藏
dyz_1984
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-2-27
收藏
 问题点数:0 回复次数:3 
为什么编译通过了却运行不了?

题目:Recaman's Sequence
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 204, Accepted users: 176
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.


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<iostream>
#define N 500000
using namespace std;
long rectman(long n)
{long a[N]={0};
if(n==0)
return 0;

a[rectman(0)]=rectman(0);
if(n>0&&rectman(n-1)-n>0&&a[rectman(n-1)-n]==0)
{a[rectman(n-1)-n]=rectman(n-1)-n;
return rectman(n-1)-n;}
else {a[rectman(n-1)+n]=rectman(n-1)+n;
return rectman(n-1)+n;}
}
int main(){
long m;

while(1)
{ cin>>m;
if(m==-1)
break;
cout<<rectman(m)<<endl;}
return 0;
}


搜索更多相关主题的帖子: 编译 运行 
2006-09-14 17:03
yvtianzll
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-9-13
收藏
得分:0 
Maybe you do not understand the problem
And there are some errors in your program
code:

#include<iostream>
#define N 500000
using namespace std;

long a[N]={0};
bool find(long n,int index)
{
for(int i =0;i<index;i++)
{
if(a[i] == n)
return true;
}
return false;

}

long rectman(long m)
{
for(long i =1;i<=m;i++)
{
if(a[i-1]-i<0||find(a[i-1]-i,i))
a[i] = a[i-1]+i;
else
a[i] = a[i-1]-i;
}

return a[m];

}
int main()
{
long m;

while(1)
{
cin>>m;
if(m==-1)
return -1;
cout<<rectman(m)<<endl;
}
return 0;
}


2006-09-14 21:01
dyz_1984
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-2-27
收藏
得分:0 

谢谢了

2006-09-15 00:23
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 

[此贴子已经被作者于2006-12-11 15:45:34编辑过]


努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-09-15 08:29
快速回复:为什么编译通过了却运行不了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041324 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved