HDU 1032 The 3n + 1 problem

http://acm.hdu.edu.cn/showproblem.php?pid=1032

可能存在i>j的情况,此情况下i,j要按原顺序输出

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int a,b,n,cnt,max,t,f;
while(~scanf("%d%d",&a,&b))
{
max=f=1;
if(a>b)
{
t=a;
a=b;
b=t;
f=0;
}
for(int i=a;i<=b;i++)
{
cnt=1;
n=i;
while(n!=1)
{
if(n%2)
n=3*n+1;
else
n/=2;
cnt++;
}
if(cnt>max)max=cnt;
}
if(f)
printf("%d %d %d\n",a,b,max);
else
printf("%d %d %d\n",b,a,max);
}
return 0;
}
原文地址:https://www.cnblogs.com/xiaohongmao/p/2438196.html