返回列表 發帖

3n+1問題

本帖最後由 李知易 於 2025-4-12 15:27 編輯

c039. 00100 - The 3n + 1 problem

參考解答:
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int a, b;
  6.     while(cin >> a >> b)
  7.     {
  8.         int Max = 0;
  9.         for(int i = ((a<b)?a:b); i <= ((a>b)?a:b); i++)
  10.         {
  11.             int n = i, cot = 1;
  12.             while(n != 1)
  13.             {
  14.                 if(n % 2 == 0) n /= 2;
  15.                 else n = n * 3 + 1;
  16.                 cot++;
  17.             }
  18.             Max = max(Max, cot);
  19.         }
  20.         cout << a << " " << b << " " << Max << endl;
  21.     }

  22.     return 0;
  23. }
複製代碼

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表