ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

6kyu Tank Truck

2019-03-01 20:55:07  阅读:684  来源: 互联网

标签:cylinder Tank get int double vt Truck 6kyu return


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.

标签:cylinder,Tank,get,int,double,vt,Truck,6kyu,return
来源: https://blog.csdn.net/Richard__Ting/article/details/88067615

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有