1005 继续(3n+1)猜想 (25 分)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//继续3n+1猜想
Scanner in = new Scanner(System.in);
int K = in.nextInt(); //测试的数
int[] a = new int[K]; //操作的数组
int[] b = new int[K]; //输入的数组
ArrayList<Integer> arr1 = new ArrayList<>(); //包含的数字(被覆盖) ArrayList是一个对象容器,对它操作时是某一个对象的增删 ,用get来获取其中的对象
ArrayList<Integer> arr2 = new ArrayList<>(); //关键字
//筛选出非关键字
for (int i = 0; i < K; i++) {
b[i] = in.nextInt();
//交给a数组进行处理(这样处理的方法很重要啊!!!)
a[i] = b[i];
while (a[i] != 1) {
if (a[i] % 2 == 0) {
a[i] = a[i] / 2;
if (!arr1.contains(a[i])) {
arr1.add(a[i]);
} else {
break; //这里是跳出循环
}
} else {
a[i] = (a[i] * 3 + 1) / 2;
if (!arr1.contains(a[i])) {
arr1.add(a[i]);
} else {
break;
}
}
}
}
//关键字
for (int j = 0; j < b.length; j++) {
if (!arr1.contains(b[j])) {
arr2.add(b[j]);
}
}
//排序
Collections.sort(arr2); //对引用类型的数据排序
if (arr2.size() == 1) {
System.out.println(arr2.get(0));
} else {
for (int t = arr2.size() - 1; t >= 0; t--) { //在格式控制上的处理,采取下面的处理-->在循环遍历输出时,当循环变量不等于在后一个变量时,输出空格,否则不输出
System.out.print(arr2.get(t));
if (t != 0) {
System.out.print(' ');
} else {
System.out.println();
}
}
}
}
}