题解 | #xxx定律#写个while循环就行
xxx定律
https://www.nowcoder.com/practice/75c189249d6145cfa33cd53edae6afc8
//Java版代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
int count = 0;
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = (3 * n + 1) / 2;
}
count++;
}
System.out.println(count);
}
}
}
#Python版代码
while True:
try:
n = int(input())
count = 0
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = (3 * n + 1) // 2
count += 1
print(count)
except:
break

查看7道真题和解析