在某国,由于能源短缺,家庭用燃气费居高不下,政府提倡每家每户节约用气,特制定了阶梯燃气费的政策:
(1)每户每月用气量在0~30立方米时,每立方米价格为5元;
(2)超过30立方米之后,每立方米价格为8元;
现在给定某户人家某月的用气量,请计算输出该户人家这月的燃气费,
输出结果显示两位小数,两位小数之后的部分四舍五入。
#include <stdio.h>
#include <math.h>
int main()
{
int n;
double m;
scanf("%d",&n);
if(n>=0&&n<=30)
m=5*n;
else
m=150+8*(n-30);
printf("%.2lf",m);
return 0;
}
其实只需要把输入变量n换成浮点型就可以了
int m=0;
#include <stdio.h>
#include<math.h>
int main()
{
double x;
scanf("%lf",&x);
if(x<1)
printf("%.2f",x);
if(x>=10)
printf("%.2f",3x-11);
if(x>=1&&x<10)
printf("%.2f",2x-1);
return 0;
}