码迷,mamicode.com
首页 > 其他好文 > 详细

05:年龄与疾病(1.6)

时间:2016-01-20 09:52:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

温馨提示:

如何输出%?

即两个%连写代表%,尝试输出printf(“%%”)

方法1:先存储数据再处理

技术分享
#include<cstdio>
int a[110];
int main(){
    int n;
    scanf("%d",&n);
    int temp;
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int x=0,y=0,z=0,t=0;
    for (int i=1;i<=n;i++){
        if  (a[i]>=0&&a[i]<=18) x++;
        if  (a[i]>=19&&a[i]<=35) y++;
        if  (a[i]>=36&&a[i]<=60) z++;
        if  (a[i]>=61) t++;
    } 
    
    a[0]=x+y+z+t;//a[0]记录总人数 
    
    printf("%.2lf%%\n%.2lf%%\n%.2lf%%\n%.2lf%%\n",double(x)/a[0]*100,double(y)/a[0]*100,double(z)/a[0]*100,double(t)/a[0]*100);// double(x)将输出结果强制转换成double,即两个%连写代表% 
    return 0;
}
View Code

 

方法2:边读边处理,定义4个变量记录4个年龄段的人数。

技术分享
//定义4个变量记录4个年龄段的人数,边读边处理 
#include<cstdio>
int main(){
    int n,x=0,y=0,z=0,t=0,sum;//x,y,z,t分别记录每个年龄段的人数 
    scanf("%d",&n);
    int temp;
    for (int i=1;i<=n;i++){
        scanf("%d",&temp);
        if  (temp>=0&&temp<=18) x++;
        if  (temp>=19&&temp<=35) y++;
        if  (temp>=36&&temp<=60) z++;
        if  (temp>=61) t++;
    } 
    sum=x+y+z+t;
    printf("%.2lf%%\n%.2lf%%\n%.2lf%%\n%.2lf%%\n",double(x)/sum*100,double(y)/sum*100,double(z)/sum*100,double(t)/sum*100);// double(x)将输出结果强制转换成double,即两个%连写代表% 
    return 0;
}
View Code

方法3:定义数组记录4个年龄段的人数和总人数

技术分享
#include<cstdio>
int a[10];
int main(){
    int n;
    scanf("%d",&n);
    int temp;
    for (int i=1;i<=n;i++){
        scanf("%d",&temp);
        if  (temp>=0&&temp<=18) a[1]++;
        if  (temp>=19&&temp<=35) a[2]++;
        if  (temp>=36&&temp<=60) a[3]++;
        if  (temp>=61) a[4]++;
    } 
    a[0]=a[1]+a[2]+a[3]+a[4];
    printf("%.2lf%%\n%.2lf%%\n%.2lf%%\n%.2lf%%\n",double(a[1])/a[0]*100,double(a[2])/a[0]*100,double(a[3])/a[0]*100,double(a[4])/a[0]*100);// double(x)将输出结果强制转换成double,即两个%连写代表% 
    return 0;
}
View Code

 

05:年龄与疾病(1.6)

标签:

原文地址:http://www.cnblogs.com/ssfzmfy/p/5144058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!