int arr[] = {10, 10, 1, 3};

假设:假设每个int都是正的。假设数组至少包含3个int
从上面数组中的三个整数中找出可以得到的最高乘积我们应该退回300英镑(10*10*3英镑)。
我想用蛮力法解决这个问题基本上,我想把每个整数乘以另一个整数,然后把这个积乘以另一个整数有谁能告诉我如何使用嵌套的3个循环来实现这一点,因为我想在尝试优化方法之前先了解如何使用蛮力。
谢谢。

最佳答案

使用三个for循环:

public static Integer highestProduct(int array[])
{
    if((array==null)||(array.length<3))
    {
        return null;
    }

    else
    {
        int max_product = Integer.MIN_VALUE;
        for(int i=0;i<array.length;i++)
        {
            for(int j=i+1;j<array.length;j++)
            {
                for(int k=j+1;k<array.length;k++)
                {
                    int product = array[i]*array[j]*array[k];
                    if(product>=max_product)
                    {
                        max_product = product;
                    }
                }
            }
        }
        return max_product;
     }
 }

关于java - 从数组中的三个整数中找到可以得到的最高乘积-如何使用蛮力求解,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44628247/

10-09 16:56
查看更多