作者howardxu (好恶)
看板Programming
标题[问题] 有关uva 100的问题
时间Tue Nov 8 14:50:50 2011
大家好~
我是第一次用uva的online judge
写程式也算是新手
这两天试着用c++写了uva编号100的程式 (3n+1那个)
自己跑的答案应该都没错
可是传到uva後他说我的程式有run time error:
Your submission with number 9449122 for the problem 100 -
The 3n + 1 problem has failed with verdict Runtime error.
This means that the execution of your program didn't finish properly.
Remember to always terminate your code with the exit code 0.
我的程式码如下:
#include <iostream>
using namespace std;
#define SIZE 1000001
static unsigned short table[SIZE]={0};
unsigned short cycleLength(unsigned long n){
cout << "counting...: " << n << endl;
if (n<SIZE && table[n]){
return table[n];
}
if (n<SIZE){
if (n&1){ //odd
table[n] = 1 + cycleLength((3*n)+1);
return table[n];
}
else{
table[n] = 1 + cycleLength(n>>1);
return table[n];
}
}
else { // over table size
if (n&1){ //odd
return 1 + cycleLength((3*n)+1);
}
else{
return 1 + cycleLength(n>>1);
}
}
}
int main(){
unsigned long in1, in2;
unsigned short temp=0;
unsigned short max;
table[1]=1;
while(cin >> in1 >> in2){
if (in1<in2){
for (int i=in1; i<=in2; i++){
temp = cycleLength(i);
if (temp > max)
max = temp;
}
}
else{
for (int i=in2; i<=in1; i++){
temp = cycleLength(i);
if (temp > max)
max = temp;
}
}
cout << in1 << " " << in2 << " " << max << endl;
max = 0;
}
return 0;
}
可以请版上的高手们帮我看看吗> <
第一次发文
如果有任何要改进的地方烦请告知
一定马上改
谢谢大家!!!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.132.237.8
1F:→ kytu:用table存的目的就是不要再算一次吧?140.112.247.191 11/08 18:40
2F:→ kytu:不过你不管怎样都会算一次~140.112.247.191 11/08 18:40
可是我有一行 if (table[n]) 这样不是如果有就会找到了吗@@
3F:→ kytu:runtime error应该是阵列读取索引值超过了~140.112.247.191 11/08 18:41
4F:推 chchwy:n给的值不超过一百万 但是计算过程中会超过 220.134.128.54 11/08 19:39
5F:→ chchwy:999999 你把它乘三再加一就超出阵列范围了 220.134.128.54 11/08 19:40
感谢两位m大 我已经把阵列范围的限制加入了 不过还是超过时间QQ
我再试试看好了 谢谢!
※ 编辑: howardxu 来自: 220.132.237.8 (11/08 22:55)
6F:→ howardxu:成功了 忘记把debug的cout拿掉= =... 220.132.237.8 11/08 23:13