我最近开始阅读S. Skiena编写的“编程挑战”一书,并且相信或不相信我有点卡在第一个问题中。为什么我的3n + 1问题解决方案错误
这里是一个解决问题的链接:3n+1 problem
这里是我的代码:
#include <stdio.h>
long get_cycle(long input){
if (input == 1){
return 1;
}
else{
if (input & 1){
return 2 + get_cycle((3*input+1)>>1);
}
else{
return 1 + get_cycle(input >> 1);
}
}
}
long get_range_cycle(int k, int j){
int i;
int max = 0;
int current_cycle;
int to = k > j ? k : j;
int from = k < j ? k : j;
for (i=from; i<=to; ++i){
current_cycle = get_cycle(i);
if (current_cycle > max){
max = current_cycle;
}
}
return max;
}
int main(){
long p, q;
long re[100][3];
int i = 0;
while (scanf("%ld %ld",&p,&q) == 2){
re[i][0] = p;
re[i][1] = q;
re[i][2] = get_range_cycle(p,q);
++i;
}
int j;
for (j=0; j<i; ++j){
printf("%ld %ld %ld\n",re[j][0],re[j][1],re[j][2]);
}
}
什么是错我的代码?输入和输出与sample完全一致。但提交结果始终是运行时错误!
“我的代码出了什么问题?”是不是一个很好的堆栈溢出问题。 –
它不会解决你的问题,但你的主要应该'返回'的东西。 – Vache
你真的应该问在uva法官论坛 –