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;
}

时间: 2024-09-20 14:41:45

HDOJ 1032(POJ 1207) The 3n + 1 problem的相关文章

HDOJ 1081(ZOJ 1074) To The Max(动态规划)

Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that re

poj 1207 The 3n + 1 problem

当我看到题目的时候我就感觉到这是一道彻彻底底的水题,因为很像A+B的作风... 但是看完题目我心里想了想:应该没有那么水吧,可能还是要优化的,暴力可能会TLE... 但是我暴力过了以后我这样想:....... 下面摘抄了一点文字说明: 大致题意: 根据给定的算法,可以计算一个整数的循环数 现在给定一个区间,计算这个区间的所有数的循环数,把最大的循环数输出 PS:输出的是整数A的循环数,而不是输出整数A 解题思路: 注意的只有一点: 输入的两个区间端点不一定是从小到大输入的,因此要先对这两个数排一

POJ 1265 Area (计算几何)(Pick定理)

Area:http://poj.org/problem?id=1265 计算几何)(Pick定理)-poj1265"> 大意:每次给你一个点的横纵坐标变化值,求有多少点在多边形上,有多少点在多边形内,和多边形的面积. 思路:Pick定理. 一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Progr

HDOJ 1716 排列2(next_permutation函数)

先讲下用这个函数来求,后面有递归的方法. 用了两种方法. 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件 下面是以前的笔记 与之完全相反的函数还有prev_permutation (1) int 类型的next_permutation int main() { int a[3]; a[0]=1;a[1]=2;a[2]=3; do { cout<<a[0]<<" "<<a[1]<<" "<<

算法:POJ 2296 Map Labeler(2-SAT+二分)

[题目大意] 坐标轴上有N个点,要在每个点上贴一个正方形,这个正方形的横竖边分别和x,y 轴平行,并且要使得点要么在正方形的上面那条边的中点,或者在下面那条边的中点,并且任意两个点的正 方形都不重叠(可以重边).问正方形最大边长可以多少? [思路] 可以很容易的看出,正 方形要么在点的上方,要么在下方,所以是用2-SAT来判断的,关键是加边的判断,要涉及到两个正方形的 位置的重叠关系比较麻烦. 然后二分正方形的边长即可. [代码] #include<iostream> #include<

HDOJ 1028 Ignatius and the Princess III(递推)

Problem Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. "The second problem is, given an positive integer N, we define an equation like this: N=a[1]+a[2]+a[3]+-+a[m];

HDOJ 1014 Uniform Generator(公约数问题)

Problem Description Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form seed(x+1) = [seed(x) + STEP] % MOD where '%' is the modulus operator. Such a function will generate pseudo-

用 eric6 与 PyQt5 实现python的极速GUI编程(系列02)---- 省市县(区)下拉列表多级联动

[概览] 本文实现如下的程序: 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到eric 6,对上一步得到的界面文件 union.ui 文件右击,编译窗体,得到 Ui_union.py 文件 4.然后再对 union.ui 文件右击,生成对话框代码,得到 union.py 文件.(在union.py中添加自己的程序逻辑) 5.py2exe打包成exe文件(此步略)   [正文] 接[概览]第4步,生成对话框代码时

前端的小玩意(9.5)——做一个仿360工具箱的web页面(完结篇,可以跑起来的工具箱)

DEMO网址: http://jianwangsan.cn/toolbox (五)添加.点击和移动的逻辑 我反思了一下,在(四)中我写的并不好,事实上,无论是大按钮,还是被添加到我的工具,或者是添加到常用工具栏,他都是一个按钮,因此,应该共享状态,即他们属于同一个tool实例,并能互相影响.   需求分析: 在重写Tool类之前,需要明确分析按钮的逻辑. 在全部工具页面: ①当按钮未被添加时,鼠标移动上去会有添加按钮显示: ②当按钮未被添加时,鼠标无论点击按钮本身还是点击添加按钮,都执行添加逻辑