简单计算空气质量指数
程序员文章站
2022-05-19 13:16:43
...
题目
空气质量指数是描述空气质量状况的无量纲指数,根据污染物的浓度换算得出。编程输入浓度C,输出指数I。其中PM2.5的浓度和级别I列表如下:
Cmin | Cmax | Imin | Imax |
---|---|---|---|
0 | 12.0 | 0 | 50 |
12.1 | 35.4 | 51 | 100 |
35.5 | 55.4 | 101 | 150 |
55.5 | 150.4 | 151 | 200 |
150.5 | 250.4 | 201 | 300 |
250.5 | 350.4 | 301 | 400 |
350.5 | 500.4 | 401 | 500 |
计算公式:
代码
#include <stdio.h>
int main(){
float c;
int count_c(float,float,float,float,float);
printf("请输入PM2.5的浓度C = ");
scanf("%f",&c);
if(c>=0&&c<=12.0) count_c(c,0,12,0,50);
else if(c>=12.1&&c<=35.4) count_c(c,12.1,35.4,51,100);
else if(c>=35.5&&c<=55.4) count_c(c,35.5,55.4,101,150);
else if(c>=55.5&&c<=150.4) count_c(c,55.5,150.4,151,200);
else if(c>=150.5&&c<=250.4) count_c(c,150.5,250.4,201,300);
else if(c>=250.5&&c<=350.4) count_c(c,250.5,350.4,301,400);
else if(c>=350.5&&c<=500.4) count_c(c,350.5,500.4,401,500);
else
printf("浓度爆表!!!!");
return 0;
}
int count_c(float c,float Cmin,float Cmax,float Imin,float Imax){
float result;
result = (Imax-Imin)/(Cmax-Cmin)*(c-Cmin) + Imin;
printf("当PM2.5的浓度为%6.2f时,对应的空气指数为%6.2f",c,result);
return 0;
}
考试版
#include "stdio.h"
typedef struct{
float cmin;
float cmax;
int lmin;
int lmax;
}pm;
void main(){
pm p[7]={0,12.0,0,50,12.1,35.4,51,100,35.5, 55.4,101,150,55.5,150.4,151,200,150.5,250.4,201,300,250.5,350.4,301,400,350.5,500.4,401,500};
float c,result;
int i;
scanf("%f",&c);
for(i=0;i<7;i++){
if(p[i].cmin<=c&&p[i].cmax>=c){
result = (p[i].lmax-p[i].lmin)/(p[i].cmax-p[i].cmin)*(c-p[i].cmin)+p[i].lmin;
printf("%6.2f",result);
}
}
}