【问题标题】:maximum volume of a box with perimeter and area given给定周长和面积的盒子的最大体积
【发布时间】:2015-05-02 02:04:57
【问题描述】:

这是问题的链接..

http://www.codechef.com/problems/J7

我发现两条边必须相等才能给出最大体积,然后用 x,x,a*x 作为三个边的长度来写方程 -

4*x + 4*x + 4*a*x = P(周长)和,

2*x^2 + 4*(a*x *x) = S(盒子的总面积)

所以从第一个方程我得到 x 的 P 和 a,然后将它代入第二个方程,然后得到一个二次方程,未知数是 a。然后我使用a的更大根得到x。 但是这种方法似乎给出了错误的答案! :| 我知道这没有任何逻辑错误。也许是一些格式错误?

这是我编写的主要代码:

{  

    public static void main(String[] args)
    {
        TheBestBox box = new TheBestBox();

        reader = box.new InputReader(System.in);
        writer = box.new OutputWriter(System.out);

        getAttributes();

        writer.flush();

        reader.close();
        writer.close();
    }

    public static void getAttributes()
    {
        t = reader.nextInt(); // t is the number of test cases in the question

        for (int i = 0; i < t; i++)
        {
            p = reader.nextInt(); // p is the perimeter given as input
            area = reader.nextInt(); // area of the whole sheet, given as input

            a = findRoot(); // the fraction by which the third side differs by the first two
            side = (double) p / (4 * (2 + a)); // length of the first and the second sides (equal)

            height = a * side; // assuming that the base is a square, the height has to be the side which differs from the other two

            // writer.println(side * side * height);

            // System.out.printf("%.2f\n", (side * side * height));

            writer.println(String.format("%.2f", (side * side * height))); // just printing out the final answer
        }
    }

    public static double findRoot() // the method to find the 2 possible fractions by which the height can differ from the other two sides and return the bigger one of them
    {
        double a32, b, discriminant, root1, root2;

        a32 = 32 * area - p * p;
        b = 32 * area - 2 * p * p;

        discriminant = Math.sqrt(b * b - 4 * 8 * area * a32);

        double temp;

        temp = 2 * 8 * area;

        root1 = (- b + discriminant) / temp; 
        root2 = (- b - discriminant) / temp;

        return Math.max(root1, root2);
    }
}

有人可以帮我解决这个问题吗?谢谢你。 :)

【问题讨论】:

  • 请清理您的问题并包括您编写的源代码。 Stack Overflow 主要用于编码问题,尽管您当然可以在这里提问。
  • ohk..我尽我所能..我还添加了源代码!这不是整个代码。不包括变量声明和输入、输出类。
  • 没有人可以帮我解决这个问题吗??

标签: rounding volume area maximize


【解决方案1】:

我也陷入了这个问题,并意识到可以通过用一侧说“l”来制作 V(体积)方程并使用微分来找到任何一侧“l”的最大体积来完成。

所以,方程式是这样的:-

P = 4(l+b+h); 
S = 2(l*b+b*h+l*h); 
V = l*b*h;

l 中的方程 V = (l^3) - (l^2)P/4 + lS/2 -------equ(1)

微分后我们得到:-

 d(V)/d(l) = 3*(l^2) - l*P/2 + S/2;

要获得最大 V,我们需要将上述等式等同于零(0)并获得 l 的值。 因此,二次方程的解将是:-

l = ( P + sqrt((P^2)-24S) ) / 24;

所以将这个 l 代入方程 (1) 以获得最大音量。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多