这题直接做就能过,但要注意由于n在变化过程中可能超过int ,导致n为负数,永远无法回到1,结果超时,所以用long long存就可以了
#include <stdio.h>
int main (){
__int64 max,now;
int a,b,count,n;
while(scanf("%d%d",&a,&b) != EOF){
n = b - a + 1;
now = a;
max = a;
count = 0;
while(n--){
max = max > now ? max : now;
while(now != 1){
if (now % 2 == 0){
now = now / 2;
count++;
}
else{
now = 3 * now + 1;
max = max > now ? max : now;
count++;
}
}
a++;
now = a;
}
printf("%d %lld\n",count,max);
}
return 0;
}
解决特定数学问题的C程序
本文介绍了一个C语言程序,用于解决一个特定的数学问题:计算从给定范围内所有整数转换到1所需的步骤,并记录过程中的最大值。特别注意了数值范围可能导致的溢出问题,使用了long long类型来确保正确性和效率。

被折叠的 条评论
为什么被折叠?



