首页 未分类

题目描述

实现不同年龄段生病的人计数,并算出百分比。

代码实现

#include <iostream>
#include <iomanip>

using namespace std;

//实现不同年龄段生病的人计数,并算出百分比 

int main ()
{
    //bing ren shu mu 
    int n;
    cin >> n;
    int a[101];
    
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    
    // range counts
    int a1,a2,a3,a4;
    a1 = 0 ,a2 = 0,a3 = 0,a4 = 0 ;
     
    int s = 0;
    for (int i=1;i<=n;i++)
    {    
        s = a[i];
        
        if(s>=0&&s<=18)
        {
            a1 += 1;
        }else if(s>=19&&s<=35)
        {
            a2 += 1;
        }else if(s>=36&&s<=60)
        {
            a3 += 1;
        }else if(s>=61)
        {
            a4 += 1;
        }
        
    }
    //cout << a1 <<a2 << a3 << a4 <<endl;
    
    
    float x1,x2,x3,x4 ;
    x1 = (a1 / (double)n )*100;
    x2 = (a2 / (double)n )*100;
    x3 = (a3 / (double)n )*100;
    x4 = (a4 / (double)n )*100;
    
    //cout << x1 << x2 << x3 << x4 <<"\t" <<endl;
    
    cout <<setiosflags(ios::fixed)<<setprecision(2)<<x1<<"% "<<endl;            
    cout <<setiosflags(ios::fixed)<<setprecision(2)<<x2<<"% "<<endl; 
    cout <<setiosflags(ios::fixed)<<setprecision(2)<<x3<<"% "<<endl;
    cout <<setiosflags(ios::fixed)<<setprecision(2)<<x4<<"% "<<endl;
    
    return 0;
} 


文章评论