Background
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.
The Problem
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.
The 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 operation overflows a 32-bit integer.
The 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
解題思考
這題在 ACM 中算是滿簡單的題目。
只要使用暴力法,照著題目給的虛擬碼實現,也就是:當 n 為奇數時,n = 3n + 1;當 n 為偶數時,n = n / 2。並使用一個變數當"計數器",每執行一次遞增1,就能得出題目要求的總執行次數。
另外,如果使用 long 型態來存 n,似乎會造成 overflow 導致程式發生執行錯誤。所以,我就乾脆直接把所有變數都宣告成 unsigned 了。 (感謝 匿名網友 指正)
要注意的是, ACM 的測試程式是一次把所有測試資料輸入,再一次接收所有輸出。並且,不能有其他多餘的輸出資料(包含 system("pause") 的輸出),並且只能用C限定的註解(/* 註解 */)。我當初錯了好幾次,花了許久才搞懂原因。
假如你跟我一樣使用的是C/C++,又覺得每次測試都要註解掉 system("pause") 相當麻煩,可以使用前置處理器 #ifndef ONLINE_JUDGE ... #endif 將之包起來。實際測試的時候,線上評測的編譯器就會跳過這個部份了。不太懂的可以參照參考解答。
參考解答(C)
#include <stdio.h> #ifndef ONLINE_JUDGE #include <stdlib.h> #endif int main(void) { unsigned long n, i, a, b, c, cycle, max; while (scanf("%ld%ld", &a, &b) == 2) { /* 一次資料輸入筆數為 2 */ printf("%ld %ld", a, b); /* 若 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; /* 每循環一次週期 + 1 */ cycle++; } /* 求出最大值 */ max = cycle > max ? cycle : max; } printf(" %ld\n", max); } #ifndef ONLINE_JUDGE system("pause"); #endif return 0; }
3 回覆:
如果给定的a,b为
113383 113383
该程序就会出错了
建议将n定义为long long类型
感謝指正。
我改成宣告為 unsigned long 型態,應該沒問題了。
張貼留言