HDU 1032 The 3n + 1 problem
来源:互联网 发布:淘宝直播达人合作 编辑:程序博客网 时间:2024/05/29 07:01
The 3n + 1 problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35818 Accepted Submission(s): 12972
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 (including the 1). 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.
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 (including the 1). 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 1,000,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.
You can assume that no opperation overflows a 32-bit integer.
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.
You can assume that no opperation overflows a 32-bit integer.
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 10100 200201 210900 1000
Sample Output
1 10 20100 200 125201 210 89900 1000 174
Source
UVA
Recommend
mcqsmall | We have carefully selected several similar problems for you: 1021 1002 1020 1017 1012
题意:输入两个整数,然后输出这两个数,这两个数和他们之间的数进行如下操作:这个数是不是偶数,如果是这个数就除以2,否则乘以3加1,直到这个数变为1结束。记录这个数变到1需要几步,包括这个数本身和变为1的那一步,可以参考题目给出的例子。然后输出这两个数及他们之间的数转换为1所需要的步骤的最大值。
推导了半天也没推导出来什么公式,然后抱着试一下的态度模拟了一下,打了一个表,没想到就过了,模拟这里有个坑,给你的两个数顺序不一定,你排一下序,然后输出的时候还得按他给你的顺序。
看了看网上的代码,没什么简单方法,不打表好像也可以过,看来是水题了。
ac代码
#include <stdio.h>long long a[1000000];int main(){long long i,j,tmp,cnt,m,n,max,l,k;for(i=1;i<1000000;i++){tmp=i;cnt=1;while(tmp!=1){if(tmp%2==0){tmp=tmp/2;cnt++;}else{tmp=tmp*3+1;cnt++;}}a[i]=cnt;}while(~scanf("%I64d%I64d",&m,&n)){l=m;k=n;if(m>n){tmp=m;m=n;n=tmp;}max=a[m];for(i=m+1;i<=n;i++){if(a[i]>max)max=a[i];}printf("%I64d %I64d %I64d\n",l,k,max);}return 0;}
1 0
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu-1032-The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- CentOS系统下使用 yum 安装 php-mysql 扩展时报错
- 安卓图片三级缓存的集装类
- 1006- Python 字典(Dictionary) items()方法
- Android文字与按钮的GrayText_Button公共类
- HDU 4607 Park Visit
- HDU 1032 The 3n + 1 problem
- Android Webview实现文件下载功能
- 11.9考试总结
- Android 优化布局层次结构
- 通过lspci如何找到驱动?
- opencv C++ Mat类的基本知识(混
- phpcms v9联动菜单前台列表调用
- 分布式系统的事务处理
- 第5章:多组件TTCN-3-笔记