HDOJ 1032(POJ 1207) The 3n + 1 problem

简介: HDOJ 1032(POJ 1207) The 3n + 1 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 classification is not known for all possible inputs.

Consider the following algorithm:

    1.       input n
    2.       print n
    3.       if n = 1 then STOP
    4.               if n is odd then   n <-- 3n+1
    5.               else   n <-- n/2
    6.       GOTO 2



Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1


It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)


Given an input n, it is possible to determine the number of numbers printed before the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.


For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.


Input


The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 10,000 and greater than 0.


You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

Output


For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input


1 10

100 200

201 210

900 1000

Sample Output


1 10 20

100 200 125

201 210 89

900 1000 174

#include <stdio.h>
#include <stdlib.h>
int arr[1000010];
int suan(int x,int num){
    if(x==1)
        return num+1;
    if(x%2==0)
        suan(x/2,num+1);
    else
        suan(3*x+1,num+1);
}
int main(){
    int m,n;
    while(scanf("%d %d",&n,&m)==2){
        int i;
        bool First=true;
        int maxx=0;
        if(n>m){
            n=n+m;
            m=n-m;
            n=n-m;
            First=false;
        }
        for(i=n;i<=m;i++){
            arr[i]=suan(i,0);
            if(maxx<arr[i])
                maxx=arr[i];
        }
        if(First)
        printf("%d %d %d\n",n,m,maxx);
        else{
            printf("%d %d %d\n",m,n,maxx);
        }
    }
    return 0;
}
目录
相关文章
|
Java 开发者
Java 面向对象新视界:揭秘子类如何“继承”父类精华,再添“创新”之笔
【6月更文挑战第16天】在Java的面向对象世界,子类继承父类的特性,如`Circle`继承`Shape`,展示“is-a”关系。子类不仅保留父类的`color`和`display`方法,还添加了`radius`属性及定制的显示逻辑。这种继承与创新允许代码复用,增强灵活性和可扩展性,使得构建复杂系统变得更加高效和模块化。通过持续的继承与定制,开发者能构建出一系列独具特色的类,充分展现面向对象编程的力量。
270 57
|
SQL 分布式计算 DataWorks
MaxCompute产品使用问题之dts是否支持传输数据到mc主键表2.0
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
180 2
|
机器学习/深度学习 人工智能 算法
通俗讲解强化学习!
强化学习这个概念是2017年Alpha Go战胜了当时世界排名第一的柯洁而被大众知道,后面随着强化学习在各大游戏比如王者荣耀中被应用,而被越来越多人熟知。王者荣耀AI团队,甚至在顶级期刊AAAI上发表过强化学习在王者荣耀中应用的论文。那么强化学习到底是什么,如何应用?下面和大家分享我对强化学习的整个过程,以及强化学习目前在工业界是如何应用的,欢迎沟通交流。
|
数据安全/隐私保护
Xshell的下载与安装+建立连接
Xshell的下载与安装+建立连接
430 0
|
IDE AliOS-Things 开发工具
如何升级HaaS100 / HaaS EDU K1的二级boot
二级boot在haas100/haaseduk1 中起到了本地升级功能实现和引导alios-things镜像的作用,在开发者使用ide的burn工具或者直接使用aos burn烧录haas100或者haaseduk1时可能会由于串口不稳定导致烧录失败,现在我们优化了一版haas100/haaseduk1的二级boot来提高烧录成功率。
如何升级HaaS100 / HaaS EDU K1的二级boot
|
Java
在Java中,负数的绝对值竟然不一定是正数!!!
绝对值是指一个数在数轴上所对应点到原点的距离,所以,在数学领域,正数的绝对值是这个数本身,负数的绝对值应该是他的相反数。
763 0
|
监控 安全 CDN
CDN 被攻击风险检查
本文主要介绍,当检测出 CDN 域名存在被攻击风险时,应如何快速检查、快速处理异常。
1617 0
|
SQL 关系型数据库 Perl
PL/SQL编程之变量
对于PL/SQL编程,准确的说oracle数据库存储过程这一部分,哎呀,当初学习的时候感觉老难了。其实很简单,就是多学几遍,学不会再学。慢慢的,就可以搞定了。 心得传授完毕,言归正传。下面来讲一下plsql变量相关的知识: 先来看一下下面这段代码: declare   v_num number(20); begin   dbms_output.put_line('请输出:'||v_num); end; / 你猜结果会怎样? 没错,没有结果。
858 0