[问题]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",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的代码,我认为跟我写的也差点儿相同。

。。。。

/*******************************************/

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

时间: 2024-11-10 07:04:41

[问题]HDOJ1032 The 3n + 1 problem的相关文章

The 3n + 1 problem

The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50648   Accepted: 16059 Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In th

POJ1207 The 3n + 1 problem

这题不是很难,模拟一下即可,但有一些细节需要注意 输入的数不一定升序,且要按原顺序输出,还有就是读完数据的问题 The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53006   Accepted: 16841 Description Problems in Computer Science are often classified as belonging to a certain cl

The 3n + 1 problem(杭电1032)(暴力求解)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23163    Accepted Submission(s): 8653 Problem Description Problems in Computer Science are often classified as belonging to a

The 3n + 1 problem(南阳oj271)(打表法)

The 3n + 1 problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorith

HDU 1032.The 3n + 1 problem【注意细节】【估计数据不强】【8月21】

The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classificat

HDU 1032 The 3n + 1 problem【递归】

/* 中文题目 3n+1问题 中文翻译-大意 当一个数n是奇数的时候变成3*n+1,为偶数的时候变成n/2 解题思路:区间内每个数逐个求解 难点详解:如何逐个计算每个数的次数,选用while循环,还有就是将此时的 i 值赋给data,用于while循环的条件.最后再将这一个数运算次数累加在一起 关键点:理解题意 解题人:lingnichong 解题时间:2014-06-03 09:40:39 解题体会:没有告诉你那个大那个小,要先比较一下数,是个陷阱 */ The 3n + 1 problem

HDU 1032 The 3n + 1 problem (这个题必须写博客)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22148    Accepted Submission(s): 8276 Problem Description Problems in Computer Science are often classified as belonging to a c

HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】

The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classificat

100 - The 3n + 1 problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36  The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a certain class of problem