在C ++中将n个实心球浸入水箱中时检查水箱是否溢出的程序
给定圆柱水箱的半径和高度,“n”个球形实心球具有半径和水箱中的水量,任务是检查将球浸入水箱后水箱是否会溢出。
计算体积的公式
圆筒
3.14 * r * r * h
其中,r是储罐半径,h是储罐高度
领域
(4/3) * 3.14 * R * R * R
其中,R是球的半径
输入项
tank_height = 5 tank_radius = 2 water_volume = 10 capacity = 10 ball_radius = 2
输出结果
It will overflow
下面使用的方法如下
输入给定的尺寸,例如槽的半径,槽的高度,要浸入的球数和半径
应用公式计算罐的容量(体积)
应用公式计算球体的体积
计算水的体积,因为只要将球浸入水箱中,水的体积就会增加。
通过将水的体积和球体的体积相加来计算总体积
检查条件以说明储罐是否会溢出
如果总体积大于容量,则储罐溢出
如果总体积小于容量,则储罐不会溢出
算法
Step 1→ declare function to 检查水箱是否会溢出
void overflow(int H, int r, int h, int N, int R)
declare float tank_cap = 3.14 * r * r * H
declare float water_vol = 3.14 * r * r * h
declare float balls_vol = N * (4 / 3) * 3.14 * R * R * R
declare float vol = water_vol + balls_vol
IF (vol > tank_cap)
Print it will overflow
End
Else
Print No it will not overflow
End
Step 2→ In main() Declare int tank_height = 5, tank_radius = 2, water_volume = 10,
capacity = 10, ball_radius = 2
call overflow(tank_height, tank_radius, water_volume, capacity, ball_radius)示例
#include <bits/stdc++.h>
using namespace std;
//检查水箱是否会溢出
void overflow(int H, int r, int h, int N, int R){
float tank_cap = 3.14 * r * r * H;
float water_vol = 3.14 * r * r * h;
float balls_vol = N * (4 / 3) * 3.14 * R * R * R;
float vol = water_vol + balls_vol;
if (vol > tank_cap){
cout<<"it will overflow";
}
else{
cout<<"No it will not overflow";
}
}
int main(){
int tank_height = 5, tank_radius = 2, water_volume = 10, capacity = 10, ball_radius = 2;
overflow(tank_height, tank_radius, water_volume, capacity, ball_radius);
return 0;
}输出结果
如果运行上面的代码,它将生成以下输出-
it will overflow