[问题]HDOJ1032 The 3n + 1 problem

http://acm.hdu.edu.cn/showproblem.php?

pid=1032
这题能够用暴力求解。求出在[ni,nj]之间全部数字产生的最大值。
通过观察能够知道,当nk靠近nj的时候,产生的值越多,于是,我认为能够在暴力的基础上做一些小的变化。降低对ni,nj中值得考查

#include <stdio.h>
int main()
{
    int m,n,i,max,count,t,j;
    while(scanf ("%d %d",&m,&n)!=EOF)
    {
        if (m<n)
        {
            t=m;
            m=n;
            n=t;
        }
        max=1;
        for (i=n;i<=m;i++)
        {
            count=1;
            j=i;    
            while (j!=1)
            {

                if (j%2!=0)
                {
                   j=j*3+1;
                   ++count;

                }
                else
                {
                   j=j/2;
                   ++count;                   
                }

                if (max<count)
                  max=count;

            }
        }

    printf ("%d %d %d
",n,m,max);    
    }
    return 0;
}

通过这段代码測试数据,发现基本上能对的上。。。。。可是HDOJ上面是Wrong Anwser。临时还没发现究竟是什么问题。。

希望看到这篇博客的各路大神能给个解答

#include<stdio.h>
int Length(int n)
{
     int k=0;
     while(n!=1)
     {
           if(n%2==1)
                 n=3*n+1;
           else
                 n/=2;
           k++;
     }
     return (k+1);
}
int main( )
{
     int i,j,temp,max,t,small,large; 
     while(scanf("%d",&i)!=EOF) 
     {
           scanf("%d",&j);
           if(i<=j)
           {
            small=i;large=j;
           }
           else { 
                 small=j;large=i;
                 }   
           temp=small;
           max=Length(temp);
           while(temp<=large)
           {
                 t=Length(temp);
                 if(t>max)
                       max=t;
                 temp++;
           }
           printf("%d %d %d
",i,j,max);
     }
     return 0;
}

这里附上别人AC的代码,我认为跟我写的也差点儿相同。

。。。。

/*******************************************/
简短挖坑,由于在网上看到了其它的费暴力解法。

原文地址:https://www.cnblogs.com/cxchanpin/p/7027019.html