总数等于100

时间:2015-10-27 22:17:32

标签: java arrays

三个数字的总和为100.这些数字是多少?数字可以是负数或正数。我尝试这样做,但程序执行得很慢

package numeros;

public class Numeros {
    public static void main(String[] args) {
        double resultado = 0;
        for (int i = -2000; i <= 2000; i++) {
            for (int j = -2000; j <= 2000; j++) {

                for (int k = -2000; k <= 2000; k++) {
                    resultado=Math.pow(i, 3)+ Math.pow(j,3) + Math.pow(k,3);
                    if(resultado==100){
                        System.out.println(i+" "+j+" "+k);
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这是一个稍微优化的版本,在我的桌面上运行大约1-2秒:

public static void main(final String[] args) {
    for (long i = -2000; i <= 2000; i++) {
        final long pi = i * i * i;
        if ((i > 0) && (pi > 100)) {
            break;
        }
        for (long j = i; j <= 2000; j++) {
            final long pj = pi + (j * j * j);
            if ((j > 0) && (pj > 100)) {
                break;
            }
            for (long k = j; k <= 2000; k++) {
                final long pk = pj + (k * k * k);
                if ((k > 0) && (pk > 100)) {
                    break;
                }

                if (pk == 100) {
                    System.out.println(i + " " + j + " " + k);
                }
            }
        }
    }
}

输出是:

-1797 -903 1870
-161 -139 190
-6 -3 7

答案 1 :(得分:1)

这是一个O(n 2 )算法。它预先计算所有多维数据集并将它们放在HashMap中,将多维数据集作为键(用于O(1)查找)并将基数作为值。我们的想法是计算sum = i 3 + j 3 并确定差异100 - sumsum - 100是否作为关键字存在在地图上。它还计算sum = i 3 - j 3 并确定这些差异100 - sumsum - 100是否作为映射中的键存在

public static void testCubes(int limit)
{
    Map<Long, Long> cubes = new HashMap<>();
    for (int i = 0; i < limit + 1; i++)
    {
        cubes.put((long) i * i * i, (long) i);
    }

    for (Map.Entry<Long, Long> e1 : cubes.entrySet())
    {
        long i = e1.getValue();
        for (Map.Entry<Long, Long> e2 : cubes.entrySet())
        {
            long j = e2.getValue();
            long sum = e1.getKey() + e2.getKey();
            long diff = 100 - sum;
            if (diff > 0 && cubes.containsKey(diff))
            {
                System.out.println("(" + i + ", " + j + ", " + cubes.get(diff) + ")");
            }
            else if (diff < 0 && cubes.containsKey(-diff))
            {
                System.out.println("(" + i + ", " + j + ", " + -cubes.get(-diff) + ")");
            }
            else if (diff == 0)
            {
                System.out.println("(" + i + ", " + j + ", 0)");
            }

            sum = e1.getKey() - e2.getKey();
            diff = 100 - sum;
            if (diff > 0 && cubes.containsKey(diff))
            {
                System.out.println("(" + i + ", " + -j + ", " + cubes.get(diff) + ")");
            }
            else if (diff < 0 && cubes.containsKey(-diff))
            {
                System.out.println("(" + i + ", " + -j + ", " + -cubes.get(-diff) + ")");
            }
            else if (diff == 0)
            {
                System.out.println("(" + i + ", " + j + ", 0)");
            }
        }
    }
}

调用testCubes(2000)会在我的电脑上产生亚秒级结果。根据订购存在重复。

(7, -3, -6)
(7, -6, -3)
(1870, -1797, -903)
(1870, -903, -1797)
(190, -139, -161)
(190, -161, -139)