`
- 浏览:
269775 次
- 性别:
- 来自:
沈阳
-
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.
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.
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
My c code:
#include<stdio.h>
int main()
{
unsigned long a,b,i,max,cycle,c,n;
while(scanf("%ld%ld",&a,&b)!=EOF)
{
printf("%ld %ld",a,b);
if(a>b)
{
c=a;
a=b;
b=c;
}
max=0;
for(i=a;i<=b;i++)
{
n=i;
cycle=1;
while(n!=1)
{
n=n%2?(3*n+1):n/2;
cycle++;
}
max=max<cycle?cycle:max;
}
printf(" %ld\n",max);
}
return 0;
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
叭啦 叭啦 巴拉 就是ACM100 3N+1 巴拉巴拉巴拉 叭啦 叭啦 巴拉 就是ACM100 3N+1 巴拉巴拉巴拉
【ACM竞赛第100题】,也称为"3n+1问题"或"Collatz猜想",是一道在算法竞赛中常见的题目。这个问题由Lothar Collatz于1937年提出,尽管简单易懂,但至今未得到完全解决。本题目的代码是用C++编写的一个解决方案,用于...
6. "1110 3n+1.txt":3n+1问题,这是一个著名的数论问题,也称为Collatz猜想,涉及到整数序列的操作。 7. "1672 TV play I.txt":电视剧I,可能涉及到时间管理、播放列表或者媒体数据处理等。 8. "1157 ...
- **3n+1问题**:1207 The 3n + 1 problem是经典的Collatz序列问题。 - **状态转移**:许多题目如1338 Ugly Numbers可能需要用到动态规划求解。 4. **递归与分治**: - **自定义数字**:1316 Self Numbers可能...
**数 A03 3n+1 数链问题** 这个问题是著名的Collatz猜想,也被称为"停机问题",它涉及到序列的迭代和数学归纳。给定一个正整数n,如果n是奇数,就乘以3再加1;如果n是偶数,就除以2。这个过程会形成一个序列,最终会...
4. **数学相关**:很多题目与数学紧密相关,例如1001 Exponentiation可能需要高效幂运算方法(如快速幂),1151 Atlantis可能涉及线性代数,1207 The 3n + 1 problem是著名的Collatz猜想,1451 T9是关于T9手机键盘的...
- 入门题目(例如3n+1问题、扫雷等)。 - 数据结构题目(例如快乐的跳跃者、扑克牌型等)。 通过以上步骤的学习和实践,大一新生不仅能够建立起坚实的编程基础,还能够在算法竞赛方面取得初步的进展。整个计划...
例如,\(f(n) = 3n - 1 \in \Theta(n)\),其中\(n_0 = 1\),\(m_1 = 2\),\(m_2 = 3\)。 2. **ܱ记号** (Big Omega): 表示一个函数的下界。如果一个函数\(f(n)\)属于\(\Omega(g(n))\),则意味着存在常数\(m\)和\(n_...
4. **(3n+1)猜想**(Basic_PAT1005):这个题目可能与著名的Collatz Conjecture(也称为3x+1猜想)有关。这个问题是数论中的一个未解之谜,要求编写程序模拟这个过程,即对于任意正整数,如果它是偶数则除以2,如果...
* 1207 The 3n + 1 problem:这是一个图论题目,要求学习者编写一个程序来解决 Collatz问题。 * 1220 Number Base Conversion:这是一个图论题目,要求学习者编写一个程序来实现数字基数转换。 贪心 贪心是POJ题目...
在第7题中,BE是∠ABC的平分线,CE是外角∠ACM的平分线,结合三角形的外角性质可以求出∠BEC的度数。 8. 两直角三角板的组合:当两个直角三角板的直角边重合时,可以形成特定的角度。第8题中,30°和45°的直角...