练习2-12 输出华氏-摄氏温度转换表 (15 分)
1. 题目摘自
https://pintia.cn/problem-sets/12/problems/245
2. 题目内容
输入2个正整数lower和upper(lower≤upper≤100),请输出一张取值范围为[lower,upper]、且每次增加2华氏度的华氏-摄氏温度转换表。
温度转换的计算公式:C=5×(F−32)/9,其中:C表示摄氏温度,F表示华氏温度。
输入格式:
在一行中输入2个整数,分别表示lower和upper的值,中间用空格分开。
输出格式:
第一行输出:"fahr celsius"
接着每行输出一个华氏温度fahr(整型)与一个摄氏温度celsius(占据6个字符宽度,靠右对齐,保留1位小数)。
若输入的范围不合法,则输出"Invalid."。
输入样例1:
32 35
输出样例1:
fahr celsius
32 0.0
34 1.1
输入样例2:
40 30
输出样例2:
Invalid.
3. 源码参考
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
double lower, upper;
cin >> lower >> upper;
if ((lower > upper) || (upper > 100))
{
cout << "Invalid." << endl;
}
else
{
cout << "fahr celsius" << endl;
for (int i = lower; i <= upper; i += 2)
{
cout.setf(ios::left);
cout.width(6);
cout << i << fixed << setprecision(1) << 1.0 * 5 * (i - 32) / 9 << endl;
}
}
return 0;
}