温度表达转化

GitHub跳转原题关系图返回列表

读入实数华氏温度,按公式 C=5×(F-32)÷9 用浮点除法算出摄氏温度,再保留 5 位小数输出。

OJ: roj

题目 ID: 1013

难度:入门

标签:入门数学python

创建: 2026-09-29 13:19

更新: 2026-10-04 22:30

题目描述

输入一个实数华氏温度 FF(F⩾−459.67F \geqslant -459.67),按公式 C=5×(F−32)÷9C = 5 \times (F-32) \div 9 计算摄氏温度并输出,精确到小数点后 5 位。输入一行一个实数,输出一行保留 5 位小数的实数。样例:输入 41,输出 5.00000。

思路

把题面公式直接翻译成浮点表达式 5 * (F - 32) / 9,用浮点除法避免整数截断。输出用 fixed 配 setprecision(5) 一步完成四舍五入与补零。

参考代码

Python 版:

python
#!/usr/bin/env python3
# Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
# rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
# rainboy的学习导航网站: https://idx.roj.ac.cn
# create_at: 2026-09-29 13:19
# update_at: 2026-09-29 13:19

import sys


def solve() -> None:
    data = iter(map(float, sys.stdin.buffer.read().split()))
    fahrenheit = next(data)  # 输入只有一个实数,按位置顺序消费
    celsius = 5 * (fahrenheit - 32) / 9  # 题面公式 C = 5×(F-32)÷9
    print(f"{celsius:.5f}")


if __name__ == "__main__":
    solve()

C++ 版(同一算法):

cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-10-04 22:30
 * update_at: 2026-10-04 22:30
 */
// main.cpp:读入华氏温度,按公式 C = 5×(F-32)÷9 换算成摄氏温度,保留 5 位小数输出。
#include <iostream>
#include <iomanip>

typedef long long ll;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    double fahrenheit = 0; // 华氏温度,题面输入是实数
    std::cin >> fahrenheit;

    double celsius = 5 * (fahrenheit - 32) / 9; // 题面公式 C = 5×(F-32)÷9

    // fixed + setprecision(5):四舍五入保留小数点后 5 位并补零
    std::cout << std::fixed << std::setprecision(5) << celsius << std::endl;
    return 0;
}