求多个圆柱体的平均体积

【输入形式】第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;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值