java实现第六届蓝桥杯奇妙的数字

奇妙的数字

奇妙的数字

小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。
你能猜出这个数字是多少吗?

请填写该数字,不要填写任何多余的内容。

结果:69

import java.util.Arrays;

public class Main {
    
    public static void main(String[] args) {
        for(long i = 10;i < 200;i++) {
            long temp1 = i * i;
            long temp2 = i * i * i;
            String A = temp1 + "";
            String B = temp2 + "";
            int len = A.length() + B.length();
            if(len == 10) {
                long[] array = new long[10];
                int j = 0;
                while(temp1 > 0) {
                    array[j++] = temp1 % 10;
                    temp1 = temp1 / 10;
                }
                while(temp2 > 0) {
                    array[j++] = temp2 % 10;
                    temp2 = temp2 / 10;
                }
                Arrays.sort(array);
                for(j = 0;j < 10;j++) {
                    if(array[j] == j)
                        continue;
                    else
                        break;
                }
                if(j == 10)
                    System.out.println("i = "+i+", i^2 = "+(i*i)+", i^3 = "+(i*i*i));
            }
        }
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/13077467.html