#30004: QQ請各位大神幫忙看一下


alviee19 (alviee19)

學校 : 不指定學校
編號 : 190227
來源 : [114.43.187.169]
最後登入時間 :
2022-10-20 19:39:19
c039. 00100 - The 3n + 1 problem -- UVa100 | From: [114.44.220.101] | 發表日期 : 2022-04-21 03:30

#include<bits/stdc++.h>

#define endl '\n'

using namespace std;

int algo(int n){

  int count = 1;

  while(n != 1){

    if(n % 2 == 1){

      n = n * 3 + 1;

      count++;

    }

    else{

      n/= 2;

      count++;

    }

  }

  return count;

}

int main() { 

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.tie(0);

  int a, b, ans = 0;

  while(cin >> a >> b){

    cout << a << ' ' << b << ' ';

    while(a <= b){

      ans = max(ans, algo(a));

      a++;

    }

    cout << ans << endl;

  }

  return 0;

}

 
#30008: Re:QQ請各位大神幫忙看一下


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c039. 00100 - The 3n + 1 problem -- UVa100 | From: [110.26.168.226] | 發表日期 : 2022-04-21 10:30

#include<bits/stdc++.h>

#define endl '\n'

using namespace std;

int algo(int n){

  int count = 1;

  while(n != 1){

    if(n % 2 == 1){

      n = n * 3 + 1;

      count++;

    }

    else{

      n/= 2;

      count++;

    }

  }

  return count;

}

int main() { 

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.tie(0);

  int a, b, ans = 0;

  while(cin >> a >> b){

    cout << a << ' ' << b << ' ';

    while(a <= b){

      ans = max(ans, algo(a));

      a++;

    }

    cout << ans << endl;

  }

  return 0;

}


1. 你的ans沒有在迴圈重設為0

2. a有可能大於b

 
ZeroJudge Forum