输入数据包含多个用例,每个用例首先包含一个整数n,然后接下来一行有n个整数a[i],其中: 1<=n<=500, 1<a[i]<=1000
请计算并输出数组a中包含的关键数,并按照其输入顺序的逆序输出,每个用例输出占一行。
3 3 8 4 5 3 8 4 7 15 5 3 8 4 15 7 0
3 15 7 3 7 15 3
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { int n = Integer.parseInt(s); if (n==0)break; String[] str = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(str[i]); } HashSet<Integer> coverset = new HashSet<>(); ArrayList<Integer> outlist = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = a[i]; while (t != 1) { if (t % 2 == 0) { t = t / 2; } else { t = (t * 3 + 1) / 2; } coverset.add(t); } } for (int i = 0; i < n; i++) { if (!coverset.contains(a[i])) outlist.add(a[i]); } for (int i = outlist.size() - 1; i >= 0; --i) { System.out.print(outlist.get(i) + " "); } System.out.println(); } } }