不光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;
}
网友评论