Description
已知华氏温度F,转换为摄氏温度C的公式为C=(F-32)*5/9。
输出给定范围(从low到high)和步长(step)的摄氏——华氏温度转换表
Input
第1行若为“C->F”表示输出:摄氏——华氏温度转换表,若为“F->C”表示输出:华氏——摄氏温度转换表。
第2、3行为两个整数:high和low,其值在-100到200之间。
第4行为step,step精确到小数点后1位。
Output
输出第一行为C和F,分别表示摄氏和华氏,与小数点对齐。若输出摄氏——华氏温度转换表,则C在前、F在后;反之,则输出华氏——摄氏温度转换表。
从输出的第2行开始为从温度low到温度high(包括low和high)的转换表,温度输出精确到小数点后1位,表格被“->”分为两个宽度相同的部分,其它的测试样例也不会给出超出宽度的数据,格式详见sample。
Sample Input
C->F
-10
40
2.5
Sample Output
C -> F
-10.0 -> 14.0
-7.5 -> 18.5
-5.0 -> 23.0
-2.5 -> 27.5
0.0 -> 32.0
2.5 -> 36.5
5.0 -> 41.0
7.5 -> 45.5
10.0 -> 50.0
12.5 -> 54.5
15.0 -> 59.0
17.5 -> 63.5
20.0 -> 68.0
22.5 -> 72.5
25.0 -> 77.0
27.5 -> 81.5
30.0 -> 86.0
32.5 -> 90.5
35.0 -> 95.0
37.5 -> 99.5
40.0 -> 104.0
HINT
输出格式可以通过sample分析出来,因为两栏的总宽度是固定的。一个隐藏的陷阱是step是浮点数,某些浮点数是无法精确存储的,因此经过一定量的计算后这个误差会影响到浮点数的相等性判断,需要加上精度控制。
Append Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char A,B;
double she,hua,end,step,kong;
scanf("%c->%c",&A,&B);
scanf("%lf%lf%lf",&kong,&end,&step);
printf(" %c -> %c\n",A, B);
if(A=='C'&&B=='F')
{
she=kong;
for(she=she;she<=end+0.01;she=she+step)//精度控制(?)
{
hua=(she*9/5+32);
printf("%5.1lf -> %5.1lf\n",she,hua);
}
}
else if(A=='F'&&B=='C')
{
hua=kong;
for(hua=hua;hua<=end+0.01;hua=hua+step)
{
she=(hua-32)*5/9;
printf("%5.1lf -> %5.1lf\n",hua,she);
}
}
return 0;
}