#include <iostream> using namespace std; int main() { int n = 0; //定义变量 int flag = 0; //步数 cin >> n; while (n != 1) { //为偶数时 if (n % 2 == 0) { n /= 2; } //为奇数时 else{ n = 3 * n + 1; } flag++; } cout << flag; system("pause"); return 0; }
#include <iostream> using namespace std; int main() { int n = 0; //定义变量 int flag = 0; //步数 cin >> n; while (n != 1) { //为偶数时 if (n % 2 == 0) { n /= 2; } //为奇数时 else{ n = 3 * n + 1; } flag++; } cout << flag; system("pause"); return 0; }