#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
a=5*(a-32)/9;
printf("Celsius = %d",a);
return 0;
}
计算摄氏温度,这样子做为什么不正确
有帮助的话 采纳一下
#include <stdio.h>
int main() {
double f, c; // 定义浮点数变量
scanf("%lf", &f); // 输入华氏温度
c = (f - 32) / 1.8; // 计算摄氏温度
printf("Celsius = %lf", c); // 输出摄氏温度
return 0;
}
你有没有考虑过华氏度是小数的情况呢?
所以你的数据类型错啦,应该用float浮点型
其次那个公式可以优化一下:原来是:5*(f-32)/9 可以优化成:(f-32)/1.8
但结果算出来的是小数,而题目要整数,怎么办呢?
可以把浮点型强制转换成整形就ok啦,AC代码如下:
#include <stdio.h>
int main()
{
float c,f;
scanf("%f",&f); //浮点型输入!
c=(f-32)/1.8; //简化版公式
printf("Celsius = %d",(int)c); //强制转换一下
return 0;
}
是不是要定义成浮点型
🍗共有N个元素,时间复杂度为O(N)
根据给出的参考资料和问题描述,可以看出摄氏温度计算公式为celsius = 5*(fahr-32)/9。从参考资料的段落0中的代码可以看出,这个计算公式是正确的。因此,根据问题给出的摄氏温度计算方法已经是正确的,结果也是正确的。以下是相应的代码示例:
#include <stdio.h>
int main() {
int fahr, celsius;
int lower, upper, step;
lower = 0; // 温度表的下限
upper = 300; // 温度表的上限
step = 10; // 步长
fahr = lower;
printf("fahr\tcelsius\n");
while (fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%4d\t%6d\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
运行以上代码,将会输出与参考资料中的结果一致的摄氏温度计算结果。
注意:可能出现摄氏和华氏小数位数不同的情况,因为在参考资料的段落1中,摄氏温度计算公式使用了浮点数运算,会得到更精确的结果。所以可以根据需要使用不同的数据类型和输出格式,来控制显示的小数位数。例如:
#include <stdio.h>
int main() {
int fahr;
int lower, upper, step;
lower = 0; // 温度表的下限
upper = 300; // 温度表的上限
step = 10; // 步长
fahr = lower;
printf("fahr\tcelsius\n");
while (fahr <= upper) {
float celsius = 5.0 / 9.0 * (fahr - 32);
printf("%4d\t%.1f\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
以上的代码指定了输出格式为1位小数的方式,以得到更加精确的温度结果。
这么改,供参考:
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
a = (int)(5.0 * (a - 32) / 9.0); //a = 5*(a-32)/9;
printf("Celsius = %d", a);
return 0;
}