标签:java
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 50648 | Accepted: 16059 |
Description
1. input n 2. print n 3. if n = 1 then STOP 4. if n is odd then n <-- 3n+1 5. else n <-- n/2 6. GOTO 2
Input
Output
Sample Input
1 10 100 200 201 210 900 1000
Sample Output
1 10 20 100 200 125 201 210 89 900 1000 174
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int Test(int n) {
int count = 0;
if (n == 1) {
return 1;
}
count++;
if (n % 2 == 0) {
count += Test(n / 2);
} else {
count += Test(3 * n + 1);
}
return count;
}
public static void printNum(int m, int n) {
int[] temp = new int[2];
temp[0] = m < n ? m : n;
temp[1] = m > n ? m : n;
int[] nums = new int[temp[1] - temp[0] + 1];
for (int i = temp[0]; i < temp[1] + 1; i++) {
nums[i - temp[0]] = Test(i);
}
Arrays.sort(nums);
System.out.print(m + " " + n + " " + nums[nums.length - 1]+"\n");
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
String str = cin.nextLine();
String[] str0 = str.split(" ");
int m = Integer.parseInt(str0[0]);
int n = Integer.parseInt(str0[1]);
printNum(m, n);
}
cin.close();
}
}
遇到了一个第一次看见的错误:Presentation Error
错误表示 运行结果正确,但是格式错误.
将该删的空格或者换行之类的去掉后就好了.
The 3n + 1 problem,布布扣,bubuko.com
标签:java
原文地址:http://blog.csdn.net/tbk345/article/details/32213435