09:年龄与疾病
总时间限制: 1000ms 内存限制: 65536kB
描述
某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理。
输入
共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
输出
每个年龄段(分四段:18以下,19-35,36-60,60-)的患病人数占总患病人数的比例,以百分比的形式输出,精确到小数点后两位(double)。
样例输入
10
1 11 21 31 41 51 61 71 81 91
样例输出
1-18: 20.00%
19-35: 20.00%
36-60: 20.00%
60-: 40.00%
提示
输出%可以用下面的语句
printf("%%");
代码
#include "pch.h"
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int n = 0, array[100]={ 0 };
float out[4] = { 0 };
cin >> n;
//接收数据
for (int i = 0; i < n; i++)
{
cin >> array[i];
if (array[i]<=18)
{
out[0] += 1;
}
else if (array[i] <= 35&& array[i] >= 19)
{
out[1] += 1;
}
else if (array[i] <= 60 && array[i] >= 36)
{
out[2] += 1;
}
else if ( array[i] >= 61)
{
out[3] += 1;
}
}
float temp = out[0] + out[1] + out[2] + out[3];
//没有采用printf,百度了一下用了下面这个
cout.setf(ios::fixed);
cout << "1-18:" << fixed << setprecision(2) << out[0]/temp *100<<"%"<< endl;
cout << "19-35:" << fixed << setprecision(2) << out[1] / temp * 100 << "%" << endl;
cout << "36-60:" << fixed << setprecision(2) << out[2] / temp * 100 << "%" << endl;
cout << "60-:" << fixed << setprecision(2) << out[3] / temp * 100 << "%" << endl;
return 0;
}