`
vcdemon
  • 浏览: 20198 次
社区版块
存档分类
最新评论
  • djx410249: 简单的自己想了几个数字测试了下,发现这个数会在经过几次跳动之后 ...
    3n+1
  • I白I: 怎么回事,好多字都卡在外面了 不显示。。。还要查看源代码看内容 ...
    3n+1

The 3n + 1 problem

    博客分类:
  • java
 
阅读更多
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 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

 

Code:

import java.util.Arrays;  
import java.util.Scanner;  
  
public class Main {  
  
    public static int Test(int n) {  
        int count = 0;  
        if (n == 1) {  
            return 1;  
        }  
        count++;  
        if (n % 2 == 0) {  
            count += Test(n / 2);  
        } else {  
            count += Test(3 * n + 1);  
        }  
        return count;  
    }  
  
    public static void printNum(int m, int n) {  
        int[] temp = new int[2];  
        temp[0] = m < n ? m : n;  
        temp[1] = m > n ? m : n;  
        int[] nums = new int[temp[1] - temp[0] + 1];  
        for (int i = temp[0]; i < temp[1] + 1; i++) {  
            nums[i - temp[0]] = Test(i);  
        }  
        Arrays.sort(nums);  
        System.out.print(m + " " + n + " " + nums[nums.length - 1]+"\n");  
    }  
  
    public static void main(String[] args) {  
        Scanner cin = new Scanner(System.in);  
        while (cin.hasNext()) {  
            String str = cin.nextLine();  
            String[] str0 = str.split(" ");  
            int m = Integer.parseInt(str0[0]);  
            int n = Integer.parseInt(str0[1]);  
            printNum(m, n);  
        }  
        cin.close();  
    }  
}  

 

 

遇到了一个第一次看见的错误:Presentation Error

错误表示 运行结果正确,但是格式错误.

将该删的空格或者换行之类的去掉后就好了.

0
0
分享到:
评论

相关推荐

    Uva 100(The 3n+1 problem) c 代码

    Uva 100 ,问题是The 3n+1 probelm ,可以ac的代码

    POJ1207-The 3n + 1 problem

    本题目的核心是解决著名的“Collatz Conjecture”问题,也被称为“3n+1猜想”。 3n+1猜想是由Lothar Collatz于1937年提出的,至今未被证明或否定。该猜想的基本规则是:对于任意正整数n,如果n为偶数,则将其除以2...

    100 - The 3n + 1 problem.c

    UVA 100题答案

    2021年全国大学生程序设计竞赛训练题.doc

    2. The 3n + 1 problem 3. Q101:The Blocks Problem 4. Q102:Ecological Bin Packing 5. Q100:考虑 a,b 之间的 2,3,5 倍数联集大小 问题 1:联集读入 在这个问题中,需要读入两个正整数 a,b,输出介于 a,b 之间...

    北大POJ题目分类,归纳等等的呢个

    6. **游戏与逻辑**:1207 The 3n + 1 problem(3n+1问题)、1664 放苹果(苹果放置问题)、1665 Biker's Trip Odometer(自行车里程计)等,这类题目往往涉及游戏规则的理解和逻辑推理。 7. **图论与网络流**:如...

    POJ ACM题目分类.

    - **3n+1问题**:1207 The 3n + 1 problem是经典的Collatz序列问题。 - **状态转移**:许多题目如1338 Ugly Numbers可能需要用到动态规划求解。 4. **递归与分治**: - **自定义数字**:1316 Self Numbers可能...

    Leetcode经典01背包-UVA_and_classic_problem:c++写的一些UVA题目的解答以及一些LeetCode的解答

    3n + 1 problem 151 - Power Crisis -&gt; 约瑟夫问题DP 问题 10607 - Joseph's Cousin -&gt; 约瑟夫问题变形 532 - Dungeon Master -&gt; BFS类型的题目 299 - Train Swapping 10038 - Jolly Jumpers 10193 - All You Need ...

    全面的ACM题目分类

    4. **数学相关**:很多题目与数学紧密相关,例如1001 Exponentiation可能需要高效幂运算方法(如快速幂),1151 Atlantis可能涉及线性代数,1207 The 3n + 1 problem是著名的Collatz猜想,1451 T9是关于T9手机键盘的...

    强大的poj分类

    - POJ 1004 - The 3n + 1 problem(3n+1问题) **相关知识点**: - 排序算法的时间复杂度分析 - 查找算法的空间效率比较 - 数学算法的应用场景及实现方法 ##### 2. 数据结构 **简介**:数据结构是计算机存储、...

    train-the-3n-1-problem-100-diego-cs:train-the-3n-1-problem-100-diego-cs,由GitHub Classroom创建

    在这个项目“train-the-3n-1-problem-100-diego-cs”中,Diego使用C++语言实现了对这个问题的编程求解。 首先,我们来理解3N+1问题的基本规则:对于任何正整数n,如果它是偶数,则将其除以2;如果它是奇数,则将其...

    On the Glide of the 3x+1 Problem

    当n为奇数时f(n)=3n+1。基于这一定义,作者提出了一种新的猜想,并给出了关于残差项的一个上界估计。 #### 关键知识点详解 ##### 3x+1问题简介 3x+1问题也称为Collatz猜想、Syracuse问题等,是由德国数学家Lothar...

    北京大学poj题目类型分类

    * 1207 The 3n + 1 problem:这是一个图论题目,要求学习者编写一个程序来解决 Collatz问题。 * 1220 Number Base Conversion:这是一个图论题目,要求学习者编写一个程序来实现数字基数转换。 贪心 贪心是POJ题目...

    POJ分类题(按照算法分类)

    15. 1207The3n+1problem:著名的3n+1猜想或Collatz问题,是一个数学问题。 16. 1218thedrunkjailer:可能是关于概率和随机过程的模拟问题。 17. 1220numberbaseconversion:关于数制转换的问题。 18. 1519...

    经典 C设计大赛案例

    2. 对于每一个数n,按照3n+1规则进行变换,并记录变换的次数。 3. 在每次变换后,检查n是否为1,如果是,则停止变换。 4. 记录每一次变换的最大次数,作为最终输出结果。 --- #### 四、The Blocks Problem **知识...

Global site tag (gtag.js) - Google Analytics