• 算法笔记_217:黑洞数(Java)


    目录

    1 问题描述

    2 解决方案

     


    1 问题描述

    任意一个5位数,比如:34256,把它的各位数字打乱,重新排列,可以得到一个最大的数:65432,一个最小的数23456。求这两个数字的差,得:41976,把这个数字再次重复上述过程(如果不足5位,则前边补0)。如此往复,数字会落入某个循环圈(称为数字黑洞)。
    比如,刚才的数字会落入:[82962, 75933, 63954, 61974] 这个循环圈。

    请编写程序,找到5位数所有可能的循环圈,并输出,每个循环圈占1行。其中5位数全都相同则循环圈为 [0],这个可以不考虑。

    循环圈的输出格式仿照:
    [82962, 75933, 63954, 61974]

    其中数字的先后顺序可以不考虑。


    2 解决方案

     1 import java.util.ArrayList;
     2 import java.util.Arrays;
     3 import java.util.Collections;
     4 import java.util.HashSet;
     5 
     6 
     7 public class Main {
     8     public static HashSet<ArrayList<Integer>> set = new HashSet<ArrayList<Integer>>();
     9     public static int start;
    10     
    11     public String getMax(int n) {
    12         StringBuffer s = new StringBuffer("");
    13         String temp = "" + n;
    14         if(temp.length() < 5) {
    15             while(temp.length() < 5) {
    16                 temp = "0" + temp;
    17             }
    18         }
    19         char[] arrayN = temp.toCharArray();
    20         Arrays.sort(arrayN);
    21         for(int i = arrayN.length - 1;i >= 0;i--)
    22             s.append(arrayN[i]);
    23         return s.toString();
    24     }
    25     
    26     public String getMin(int n) {
    27         String temp = getMax(n);
    28         StringBuffer s = new StringBuffer(temp);
    29         return s.reverse().toString();
    30     }
    31     
    32     public int getResult(int n) {
    33         int max = Integer.valueOf(getMax(n));
    34         int min = Integer.valueOf(getMin(n));
    35         return max - min;
    36     }
    37     
    38     public static void main(String[] args) {
    39         Main test = new Main();
    40         for(int i = 10000;i < 100000;i++) {
    41             if(i % 11111 == 0)
    42                 continue;
    43             ArrayList<Integer> list = new ArrayList<Integer>();
    44             int a = i;
    45             while(true) {
    46                 a = test.getResult(a);
    47                 if(!list.contains(a))
    48                     list.add(a);
    49                 else
    50                     break;
    51             }
    52             start = list.indexOf(a);
    53             ArrayList<Integer> temp = new ArrayList<Integer>();
    54             for(int j = start;j < list.size();j++)
    55                 temp.add(list.get(j));
    56             Collections.sort(temp);
    57             set.add(temp);
    58         }
    59         for(ArrayList<Integer> list : set)
    60             System.out.println(list);
    61     }
    62 }

    运行结果:

    [62964, 71973, 74943, 83952]
    [53955, 59994]
    [61974, 63954, 75933, 82962]
  • 相关阅读:
    Nginx解决跨域
    子网掩码的作用
    并发与并行
    Java8 parallelStream与迭代器Iterator性能
    Spring Data MongDB空间索引(判断一个点Point是否在一个区域Polygon内)
    BeanFactory的实现原理
    序列化以及反序列化
    MongoDB用户名和密码
    Cannot assign to 'self' outside of a method in the init family
    OC方法和文件编译
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/6890850.html
Copyright © 2020-2023  润新知