这样呢?
#include <stdio.h>
#define M 1000001
int ss[M] = {0};
int fun(long long n)
{
if (1 == n || n < M && ss[n]) return ss[n];
int tmp = n % 2 ? fun(n * 3 + 1) : fun(n / 2);
if (n < M) ss[n] = tmp + 1;
return tmp + 1;
}
int main()
{
int l, h, v, s, tmp;
while (2 == scanf("%d%d", &l, &h) && l && h)
{
s = 0, v = l;
if (l > h) tmp = l, l = h, h = tmp;
for (int i = l; i <= h; ++i)
{
if(!ss[i]){
fun(i);
}
if (s < ss[i]) s = ss[i], v = i;
}
printf("Between %d and %d, %d generates the longest sequence of %d values.\n", l, h, v, s);
}
return 0;
}
#include <stdio.h>
#define M 1000001
int ss[M] = {0};
int fun(long long n)
{
if (1 == n || n < M && ss[n]) return ss[n];
int tmp = n % 2 ? fun(n * 3 + 1) : fun(n / 2);
if (n < M) ss[n] = tmp + 1;
return tmp + 1;
}
int main()
{
int l, h, v, s, tmp;
while (2 == scanf("%d%d", &l, &h) && l && h)
{
s = 0, v = l;
if (l > h) tmp = l, l = h, h = tmp;
for (int i = l; i <= h; ++i)
{
if(!ss[i]){
fun(i);
}
if (s < ss[i]) s = ss[i], v = i;
}
printf("Between %d and %d, %d generates the longest sequence of %d values.\n", l, h, v, s);
}
return 0;
}