參考程序1:
#include <cstdio>
using namespace std;int main() {double K;scanf("%lf", &K);double C = K - 273.15; //轉換為攝氏溫度 double F = 32 + C * 1.8; //轉換為華氏溫度 if (F > 212) //條件判斷 printf("Temperature is too high!\n"); elseprintf("%.2f %.2f\n", C, F); // printf格式輸出C F 小數點后2位,兩個數字中間由一個空格 return 0;
}
參考程序2:
#include <iostream>
#include <iomanip> // 使用 setprecisionusing namespace std;int main() {double K;cin >> K; // 輸入開爾文溫度double C = K - 273.15; // 攝氏溫度double F = C * 1.8 + 32; // 華氏溫度if (F > 212) {cout << "Temperature is too high!" << endl;} else {// 輸出保留兩位小數,固定小數格式cout << fixed << setprecision(2) << C << " " << F << endl;}return 0;
}