- 1 import java.util. * ;
- 2 3 public class Main 4 {
- 5 static Scanner scan = new Scanner(System. in );
- 6 public static void main(String[] args) 7 {
- 8 int[] flag = new int[110];
- 9
- for (int i = 0; i < 110; ++i) 10 flag[i] = -1;
- 11 int k = scan.nextInt();
- 12
- while (k--!=0) 13 {
- 14 int t = scan.nextInt();
- 15
- if (flag[t] == -1) 16 {
- 17 flag[t] = 0;
- 18
- while (t != 1) 19 {
- 20
- if (t % 2 == 1) 21 {
- 22 t = (3 * t + 1) / 2;
- 23
- if (t < 101) //在公式的递推中,n远大于100,测试时数组为500仍然越界,但是大于100的数据我们可以不理会
- 24 flag[t] = 1;
- 25
- }
- 26
- else 27 {
- 28 t /= 2;
- 29
- if (t < 101) //!!!此处的n可能因为上次奇数的计算即使/2后仍然越界
- 30 flag[t] = 1;
- 31
- }
- 32
- }
- 33
- }
- 34
- }
- 35 36 boolean b = true;
- 37
- for (int j = 101; j > 1; --j) 38 {
- 39
- if (flag[j] == 0) 40 {
- 41
- if (b) 42 {
- 43 System.out.print(j);
- 44 b = false;
- 45
- }
- 46
- else 47 {
- 48 System.out.print(" " + j);
- 49
- }
- 50 51
- }
- 52
- }
- 53
- }
- 54
- }
来源: