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 check whether tank will overflow or not 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; //check whether tank will overflow or not 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
广告