6kyu Tank Truck

6kyu Tank Truck

文章目录

Problem

To introduce the problem think to my neighbor who drives a tanker truck. The level indicator is down and he is worried because he does not know if he will be able to make deliveries. We put the truck on a horizontal ground and measured the height of the liquid in the tank.

Fortunately the tank is a perfect cylinder and the vertical walls on each end are flat. The height of the remaining liquid is h, the diameter of the cylinder is d, the total volume is vt (h, d, vt are positive or null integers). You can assume that h <= d.

Could you calculate the remaining volume of the liquid? Your function tankvol(h, d, vt)returns an integer which is the truncated result (e.g floor) of your float calculation.

Examples

tankvol(40,120,3500) should return 1021 (calculation gives about: 1021.26992027)

tankvol(60,120,3500) should return 1750

tankvol(80,120,3500) should return 2478 (calculation gives about: 2478.73007973)
  • Tank vertical section:
    6kyu Tank Truck

Solutions

#include<cmath>
class VolTank
{
public:
    static int tankVol(int h, int d, int vt){
      
      double pi = acos(-1); // get the value of pi
      double length = (double)(4*vt)/(d*d*pi); // get the length of the cylinder
      double v = 0; // return the answer v
      
      // two situations
      if(2*h <= d){
          double theta = acos((double)(d-2*h)/d); // get the value of theta
          double smax = 0.25*d*d*theta; // get the volume of cylinder(arc)
          double smin = 0.125*d*d*sin(2*theta); // get the volume of cylinder(triangle)
          v = (smax-smin)*length;
      } else { // 2*h > d
          double theta = acos((double)(2*h-d)/d); // get the value of theta
          double smax = 0.25*d*d*theta; // get the volume of cylinder(arc)
          double smin = 0.125*d*d*sin(2*theta); // get the volume of cylinder(triangle)
          v = vt - (smax-smin)*length;          
      }
      
      int ans = int(v); // return the answer ans
      return (ans);
    }
};

Tips

  • Focus on the two situations: whether 2*h > d or not?
  • When programming, pay attention to cast the value.
  • Don’t forget the formulas of how to calculate arc area and triangle area.
  • Be careful.
上一篇:英语四6级CET6资料大学六级单词


下一篇:VT在BIOS内已开启,但CPU-v显示未开启