3n+1问题
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
0
-
描述
-
对于任意大于一的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半,经过若干次这样的变换,一定会使n变为1.求输出变换的次数,注 要求次数要对3取余;例如3->10->5->16->8->4->2->1 变换了7次,对3取余的1; n<=109
-
输入
- 测试数据有多组 输出
- 每次输出占一行 样例输入
-
3 2
样例输出
-
1 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
long number = scanner.nextLong();
int count = 0;
while (number != 1) {
count++;
if (number % 2 == 1) {
number = number * 3 + 1;
} else {
number = number / 2;
}
}
System.out.println(count % 3);
}
}
}