PAT B1001 分支中冗余代码的提取

2019-07-14  本文已影响0人  ranerr_

不光step++可以提取到外面,n\=2也可以提取

#include <iostream>
using namespace std;
int main() {
  int n, step = 0;
  cin >> n;
  while (n != 1) {
    if (n % 2) {
      n = (3 * n + 1) / 2;
    }
    else {
      n /= 2;
    }
    step++;
  }
  cout << step;
  return 0;
}
#include <iostream>
using namespace std;
int main() {
  int n, step = 0;
  cin >> n;
  while (n != 1) {
    if (n % 2) n = (3 * n + 1);
    n /= 2;
    step++;
  }
  cout << step;
  return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读