将八进制小数看成以 8 的幂为分母的精确值,再用 Decimal 输出。
OJ: noi_openjudge
题目 ID: ch0113-03
难度:普及-
标签:进制转换高精度python
日期: 2026-07-30 23:01
题意
将
思路
小数点后的 Decimal 配合足够精度可避免二进制浮点产生的尾差,最后去掉末尾零。
代码
Python代码
python
from decimal import Decimal, getcontext
getcontext().prec = 60
octal_number = input()
fractional_digits = octal_number.split(".")[1]
numerator = int(fractional_digits, 8)
value = Decimal(numerator) / (Decimal(8) ** len(fractional_digits))
decimal_number = format(value, "f").rstrip("0")
print(f"{octal_number} [8] = {decimal_number} [10]")复杂度
时间复杂度为
总结
进制小数的位数对应分母的幂次,先写成分数最容易保证精确性。