-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.java
65 lines (57 loc) · 1.83 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pat.pat1005;
import java.util.*;
/**
* 1005 继续(3n+1)猜想 (25分)
* <p>
* 题目描述: https://pintia.cn/problem-sets/994805260223102976/problems/994805320306507776
*
* @author yangyi
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//第一行第 1 行给出一个正整数 K (<100)
int k = scanner.nextInt();
//第 2 行给出 K 个互不相同的待验证的正整数 n (1<n≤100)的值
List<Integer> targetNumbers = new LinkedList<>();
for (int i = 0; i < k; i++) {
//3 5 6 7 8 11
targetNumbers.add(scanner.nextInt());
}
//对K进行计算并保存结果
Set<Integer> temps = new LinkedHashSet<>();
for (Integer targetNumber : targetNumbers) {
while (targetNumber != 1) {
if (targetNumber % 2 == 0) {
targetNumber /= 2;
} else {
targetNumber = (3 * targetNumber + 1) / 2;
}
//暴力穷举罗列所有可能
temps.add(targetNumber);
}
}
List<Integer> result = new ArrayList<>();
for (Integer targetNumber : targetNumbers) {
if (temps.contains(targetNumber)) {
continue;
}
result.add(targetNumber);
}
result.sort((o1, o2) -> {
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
}
return 0;
});
for (int i = 0; i < result.size(); i++) {
if (i != result.size() - 1) {
System.out.print(result.get(i) + " ");
} else {
System.out.println(result.get(i));
}
}
}
}