使用真除法计算 a/b,并以 .9f 输出九位小数。
OJ: noi_openjudge
题目 ID: ch0103-05
难度:入门
标签:浮点数python
日期: 2026-07-30 23:01
题意
把两个整数视为分子和非零分母,输出分数的小数值,保留 9 位小数。
思路
Python 的 / 总是进行真除法,结果为浮点数。f-string 的 .9f 同时完成四舍五入和末尾补零。
代码
Python代码
python
numerator, denominator = map(int, input().split())
print(f"{numerator / denominator:.9f}")C++代码
cpp
#include <cstdio>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%0.9lf\n",a*1.0/b);
return 0;
}复杂度
时间复杂度和额外空间复杂度均为
总结
需要固定小数位时使用 .nf,不要依赖浮点数的默认输出。