编译出现runtime error的提示,望解答
题目: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.