注册 登录
编程论坛 C++教室

编译出现runtime error的提示,望解答

MJFW 发布于 2008-12-03 23:20, 981 次点击
题目:
  Recaman's Sequence
Time Limit:3000MS  Memory Limit:60000K
Total Submit:1656 Accepted:533

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>
using namespace std;
void process(long n)
{
    long *a,*b,k,temp;
    a=new long[500001];
    b=new long[3011600];
    a[0]=0;
    b[0]=1;
    for(k=1;k<=n;k++)
    {
        temp=a[k-1]-k;
        if(temp>0)
        {
            if(b[temp]==1)
            {
                a[k]=a[k-1]+k;
                b[a[k]]=1;
            }
            else
            {
                a[k]=a[k-1]-k;
                b[temp]=1;
            }
        }
        else
        {
            a[k]=a[k-1]+k;
            b[a[k]]=1;
        }
    }
    
    cout<<a[k-1]<<endl;
    delete []a;
    delete []b;
}
int main(void)
{
    int i=-1,j;
    long n,math[100];
    do
    {
        i++;
        cin>>math[i];
        if(math[i]<-1||math[i]>500000)
        {i--;}
    }while(math[i]!=-1);
    for(j=0;j<i;j++)
    {
        n=math[j];
        process(n);
    }
    return 0;
}
结果在下面这个网站上提交编译时说Runtime Error,这题的题号是:2081
http://ai.pku.
3 回复
#2
debroa7232008-12-04 21:33
调试了半天也没看到有Runtime Error,可能出错的的地方也就是数组下标过界,但下标都控制的很好,不会出界.猜想可能引起错误的原因:   
a=new long[500001];
b=new long[3011600];
由于分配空间过大引想错误,猜想可能是在内存极小的机子上运行了你的代码,没有足够的空间运行程序吧.上面分配的空间大概在十几M,虚拟内存也有十几M,加起来三十多M.当然也有可能是系统问题,可能是其它的操作系统上的内存分配机制和WINDOWS不同,或是早期的操作系统,对这个我并不熟悉,看有没有其他人来解答一下.
#3
dillon2008-12-05 10:11
LZ是在做在线评定。那个网站应该有关于这个错误的解释啊。LZ仔细找下。
#4
ronaldowsy2008-12-05 11:25
这不是ACM上面的题嘛?呵呵!
1