【发布时间】: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