[问题]HDOJ1032 The 3n + 1 problem

来源:互联网 发布:网络礼仪作文 编辑:程序博客网 时间:2024/05/21 06:17

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",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\n",i,j,max);     }     return 0;}

这里附上别人AC的代码,我觉得跟我写的也差不多。。。。。

/*******************************************/
简短挖坑,因为在网上看到了其他的费暴力解法。

1 0