#37894: AC C++


1018harris1018@gmail.com (Harris Chen)

學校 : 不指定學校
編號 : 229825
來源 : [61.230.224.29]
最後登入時間 :
2024-10-20 19:08:01
c039. 00100 - The 3n + 1 problem -- UVa100 | From: [118.161.96.58] | 發表日期 : 2023-10-16 21:05

include <iostream>
using namespace std;
int main() {
  int n, a, b;
  int max = 0;
  int count = 1;
  while (cin >> a >> b) {
    max=0;
    if (a <= b) {
      for (int i = a; i <= b; i++) {
        n = i;
        count = 1;
        while (n != 1) {
          count++;
          if (n == 1) {
            break;
          } else if (n % 2 != 0)
            n = 3 * n + 1;
          else
            n = n / 2;
        }
        if (max < count)
          max = count;
      }
    }
    if (b < a) {
      for (int i = b; i <= a; i++) {
        n = i;
        count = 1;
        while (n != 1) {
          count++;
          if (n == 1) {
            break;
          } else if (n % 2 != 0)
            n = 3 * n + 1;
          else
            n = n / 2;
        }
        if (max < count)
          max = count;
      }
    }
    cout << a << " " << b << " " << max << endl;
  }
}
 
ZeroJudge Forum