吃了3個WA以後,去找解答才知道這題有兩個陷阱= =
(1) L有可能大於H
(2) 題目說不會大於long的意思是指不會大於unsigned long
#include <stdio.h>
int main(void)
{
long long L, H, n, num;
int c, max;
while (scanf("%lld%lld", &L, &H) == 2 && (L || H))
{
if (L > H)
{
n = H;
H = L;
L = n;
}
printf("Between %lld and %lld, ", L, H);
max = 0;
for (; L <= H; L++)
{
c = 0;
n = L;
do {
if (n % 2)
n = 3*n+1;
else
n /= 2;
c++;
} while (n != 1);
if (c > max)
{
max = c;
num = L;
}
}
printf("%lld generates the longest sequence of %d values.\n", num, max);
}
return 0;
}
沒有留言:
張貼留言