参考代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
cin >> n;
int age;
int count[4] = {0}; // 用于统计四个年龄段的患病人数
for (int i = 0; i < n; ++i) {
cin >> age;
if (age >= 0 && age <= 18) {
count[0]++;
} else if (age >= 19 && age <= 35) {
count[1]++;
} else if (age >= 36 && age <= 60) {
count[2]++;
} else if (age >= 61) {
count[3]++;
}
}
// 计算每个年龄段患病人数占总患病人数的比例,并以百分比形式输出
int total = count[0] + count[1] + count[2] + count[3];
double ratio[4] = {(double)count[0] / total, (double)count[1] / total, (double)count[2] / total, (double)count[3] / total};
cout << fixed << setprecision(2);
for (int i = 0; i < 4; ++i) {
cout << ratio[i] * 100 << "%" << endl;
}
return 0;
}
0.0分
1 人评分