#include <stdio.h>
int main()
{
printf("请分别输入身高的英尺和英寸,"
"如输入5 7则表示5英尺7寸");
int foot;
int inch;
scanf("%d %d",&foot,&inch);
printf("身高是%f米.\n",
((foot+inch/12)*0.3048));
//(5+7÷12)×0.3048=1.7018米
return 0;
}
上述结果并不正确
因为两个整数的运算结果只能是整数,如有小数会自动舍弃
方法1:
将参与运算的数改为小数即可
#include <stdio.h>
int main()
{
printf("请分别输入身高的英尺和英寸,"
"如输入5 7则表示5英尺7寸");
int foot;
int inch;
scanf("%d %d",&foot,&inch);
printf("身高是%f米.\n",
((foot+inch/12.0)*0.3048)); 改此行
//(5+7÷12)×0.3048=1.7018米
return 0;
}
方法2:
将int改为double(双浮点数)
将scanf中的%d改为%lf,将printf中%d改为%f
#include <stdio.h>
int main()
{
printf("请分别输入身高的英尺和英寸,"
"如输入5 7则表示5英尺7寸");
double foot;
double inch;
scanf("%lf %lf",&foot,&inch);
printf("身高是%f米.\n",
((foot+inch/12.0)*0.3048));
//(5+7÷12)×0.3048=1.7018米
return 0;
}
重点
int -*> printf("%d"),scanf("%d")
double --> printf("%f"),scanf("%lf")
#include <stdio.h>
int main()
{
int cm=0;
scanf("%d",&cm);
int foot=cm/30.48; //"/"作用a/b取整
printf("%d",foot);
int inch=((cm/30.48)-foot)*12;
printf("%d %d",foot,inch);
return 0;
}