【输入形式】第1行输入一个正整数n(1<-n<-100),表示圆柱体的数量。接下来连续输入n行,表示n个圆柱体的参数信息,每行输入2个正的浮点数(以一个空格隔开),分别表示当前圆柱体的底面半径和高。
【输出形式】输出1个浮点数(小数点后保留2位),表示n个圆柱体的平均体积。
【样例输入】R1.1 2 23.3 4.45.56.6
【样例输出】262.04
【样例说明】3个圆柱体,第1个圆柱体的底面半径为11、高为2.2,第2个圆柱体的底面半径为3.3、高为4.4,第3个圆柱体的底面半径为5.5、高为66,则3个圆柱体的平均体积为262.04。
#include <iostream>
#include <iomanip>
using namespace std;
const double PI = 3.14159265359;
class Cylinder {
private:
double radius;
double height;
public:
Cylinder(){}
Cylinder(double r, double h) : radius(r), height(h) {}
double getVolume() {
return PI * radius * radius * height;
}
};
void inputArray(Cylinder* cylinders, int n) {
for (int i = 0; i < n; ++i) {
double radius, height;
cin >> radius >> height;
cylinders[i] = Cylinder(radius, height);
}
}
double meanVolume(Cylinder* cylinders, int n) {
double totalVolume = 0.0;
for (int i = 0; i < n; ++i) {
totalVolume += cylinders[i].getVolume();
}
return totalVolume / n;
}
int main() {
int n;
cin >> n;
Cylinder* cylinders = new Cylinder[n];
inputArray(cylinders, n);
cout << fixed << setprecision(2);
cout << meanVolume(cylinders, n) << endl;
delete[] cylinders;
return 0;
}