6kyu Tank Truck

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Richard__Ting/article/details/88067615

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:
    在这里插入图片描述

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.

猜你喜欢

转载自blog.csdn.net/Richard__Ting/article/details/88067615