#include<stdio.h>
void main()
{
int taxitype,money;
float s;
printf("Input taxitype(0,1):");
scanf("%d",&taxitype);
printf("Input s:");
scanf("%f",&s);
if(s<3)
money = 8;
else if(taxitype==0)
money =8+(s-3)*1.5;
else
money =8+(s-3)*2;
printf("money=%d\n",money);
}
原题是叫我四舍五入打车的钱 我这样一算尾数不管是几都舍去了 不存在入的问题😭
浮点型数据直接转int整型数据会直接舍弃小数后所有
你可以先用float res变量计算结果 然后转money 让计算的res - money判断是否小于0.5 如果不小于则money+=1
这么改下,供参考:
#include<stdio.h>
void main()
{
int taxitype;
float s , money; //修改
printf("Input taxitype(0,1):");
scanf("%d",&taxitype);
printf("Input s:");
scanf("%f",&s);
if(s<3)
money = 8;
else if(taxitype==0)
money =8+(s-3)*1.5;
else
money =8+(s-3)*2;
printf("money=%.0f\n",money); //修改
}