The 3n + 1 Problem
UVa 100 UVa Online Judge ↗
在 [i, j] 區間內,取每個數最長的 3n+1 序列;特別要注意的是 i 可能大於 j。
| Attempts | 1 |
|---|---|
| First attempt | 2026-08-17 |
| Solved | 2026-08-17 |
Problem
給定一區間 [i, j],依照題目所給的演算法,求出區間內循環次數 (cycle-length) 最多的數字。
formalization 之後,其實可以寫成數學式:
Approach
這題其實只需照著題目給的 pseudocode 刻出來就會過。我們可以設定一個變數以存放最長的循環次數,若大於當前的循環次數,則將變數更新。
-
輸出順序:測資可能給出 。此外要先照原本順序印出 和 ,接著排序 後再進入迴圈(順序印反會 WA)。
-
迴圈變數:利用 temp 變數來存放 n 做運算,避免直接更動到迴圈變數,造成無窮迴圈。
Solution
#include <iostream>
using namespace std;
void solve() { int i, j;
while (cin >> i >> j) { cout << i << " " << j << " ";
if (i > j) { swap(i, j); }
int max_Cycle = 0;
for (int k = i; k <= j; k++) { int cycle = 1; int temp = k;
while (temp != 1) { if (temp % 2 == 1) { temp = temp * 3 + 1; } else { temp /= 2; } cycle++; }
max_Cycle = max(max_Cycle, cycle); }
cout << max_Cycle << '\n'; }}
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
solve();
return 0;}Sample Input
1 10100 200201 210900 1000Sample Output
1 10 20100 200 125201 210 89900 1000 174Pitfalls
錯誤歷程
- AC一次直接過#1
See also
- 考拉茲猜想(Collatz conjecture)