java 检查一个数字的平方中的数字是否不在其立方中

标签 java arrays

定义一个正数,如果其平方中的数字都不在其立方中,则该正数将被隔离。例如,69 是 n 个孤立数,因为 163*163 = 26569 和 163*163*163 = 4330747 并且正方形不包含立方体中使用的数字 0、3、4 和 7 中的任何数字。另一方面,162 不是一个孤立的数字,因为 162*162=26244 和 162*162*162 = 4251528 以及出现在正方形中的数字 2 和 4 也在立方体中。

编写一个名为 isIsolated 的函数,如果参数是孤立数,则返回 1;如果不是孤立数,则返回 0;如果无法确定是否是孤立数,则返回 -1。

我已经尝试过这个:

public static int isIsolated (int n) {
        int square = n*n;
        int cube   = n*n*n;
        int[] a = new int[7];
        int[] b = new int[7];

        if(n < 2097151 || n>1){         
            for(int i=6;i>=0;i--){
                int k = square % 10;
                int l = cube % 10;

                a[i]= k;
                b[j]=l;

                square = square/10; 
                cube = cube/10;
            }

            for(int j=a.length-1;j>=0;j++)
            {
                for(int m=0;m<j;m++)
                {
                    if(a[j] == b[m])
                        return 0;
                    else
                        return 1;
                }
            }
        }
        return -1;
    }

但它没有按预期工作。

我是初学者,请帮助我。

最佳答案

您的代码存在许多问题。

一旦数字耗尽,您就将 0 放入数组中,因此任何少于 7 位的数字最终都会在数组中至少有一个 0 并失败你的测试。

您在数组中保留了默认的 0,因此任何在正方形或立方体中带有 0 的数字都会出错。此代码建议其立方体适合 long 的最高孤立数字是:

31563 is isolated - ^2 = 996222969 ^3 = 31443785570547

// Max digits.
private static final int DIGITS = 20;
// Indicates an empty digit.
private static final int EMPTY = -1;
// Max value I can handle.
private static final long MAX = (long) Math.cbrt(Long.MAX_VALUE);

public static int isIsolated(long n) {

    if (n > 1 && n < MAX) {
        long square = n * n;
        long cube = n * n * n;
        long[] a = new long[DIGITS];
        Arrays.fill(a, EMPTY);
        long[] b = new long[DIGITS];
        Arrays.fill(b, EMPTY);
        for (int i = 0; i < DIGITS; i++) {
            if (square > 0) {
                a[i] = square % 10;
                square = square / 10;
            }
            if (cube > 0) {
                b[i] = cube % 10;
                cube = cube / 10;
            }
        }

        for (int i = 0; i < DIGITS; i++) {
            if (a[i] != EMPTY) {
                for (int j = 0; j < DIGITS; j++) {
                    if (a[i] == b[j]) {
                        return 0;
                    }
                }
            }
        }
        return 1;
    }
    return -1;
}

public void test(int n) {
    System.out.println(n + " is " + (isIsolated(n) == 1 ? "" : "not ") + "isolated");
}

public void test() {
    System.out.println("Hello");
    test(1234);
    test(69);
    test(162);
    for (long i = 0; i < MAX; i++) {
        if (isIsolated(i) == 1) {
            System.out.println(i + " is isolated - ^2 = " + (i * i) + " ^3 = " + (i * i * i));
        }
    }
}

关于java 检查一个数字的平方中的数字是否不在其立方中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151982/

相关文章:

java - Java中无法存储多个输入值

java - list 中引用的资源不能因配置而异?

python - Numpy:对 NxM 数组的列(或行)的操作

javascript - 在 Javascript 中随机播放嵌套数组

java - java中无法动态增加数组的大小

java - 建立套接字连接时出错

java - 将数组 vector 转换为一个数组

java - 如何在一条 Java 语句中连接静态最终字符串数组

java - 从泛型类返回一个数组。如何在 main 方法中打印数组中的数据?

VIVADO HLS的C语言-如何将Pseudo_random二进制序列存储并生成到memcpy中