Canna

Tuesday, 25 September 2018

Rectangular Object

  • Problem Description

    Subbu needs to make a rectangular box for his physics class project. He has bought P cm of wire and S cm2 of special paper. He would like to use all the wire (for the 12 edges) and paper (for the 6 sides) to make the box.

    What is the largest volume of the box that Johnny can make?
    Input

    The first line contains t, the number of test cases (about 10). Then t test cases follow.

    Each test case contains two integers P and S in a line (1<= P <= 40000, 1 <= S <= 20000). You may assume that there always exists an optimal solution for the given input cases.
    Output

    For each test case, print a real number that is the largest volume of the box that Johnny can make, rounded to two decimal places.
  • Test Case 1

    Input (stdin)
    2
    
    20 14
    
    20 16
    
    
    Expected Output
    3.00
    
    4.15
  • Test Case 2

    Input (stdin)
    2
    
    40 20
    
    30 15
    
    
    Expected Output
    2.64
    
    2.02
  • Program
  • #include<stdio.h>
    #include<math.h>
    int main()
    {
    
        int t,p,s;
        float v,h,d;
        scanf("%d",&t);
        while(t--){
            scanf("%d %d",&p,&s);
            d=sqrt(p*p-24*s);
            h=(p-d)/12;
            v=(h*h*h)-((h*h*p)/4)+((s*h)/2);
            printf("%.2f\n",v);
        }return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.