C语言网-1095题
作者:互联网
import java.util.Scanner;
public class Practice_1006 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int left_value = scanner.nextInt();
int right_value = scanner.nextInt();
int max_length = 0;
int left = left_value, right = right_value;
if (left > right) { //这是为了保证区间左小右大
int t = left;
left = right;
right = t;
}
for (int i = left; i <= right; i++) {
if (countLength(i) > max_length) {
max_length = countLength(i);
}
}
System.out.println(left_value + " " + right_value + " " + max_length);
}
}
public static int countLength(int n) {
int count = 1;
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
count++;
} else {
n = n * 3 + 1;
count++;
}
}
return count;
}
}
记得把问题分解开来,尽量不要所有代码都挤在一个main里,这样自己会被自己弄晕。
标签:1095,right,scanner,int,value,C语言,length,left 来源: https://www.cnblogs.com/blog-of-guoqi/p/11569500.html