按题面固定的 π=3.14159 套公式 2r、2πr、πr²,用 printf 的 %.4f 统一保留 4 位小数,常量只需定义一次。
OJ: roj
题目 ID: 1014
难度:入门
标签:输入输出数学python
创建: 2026-09-29 13:19
更新: 2026-10-04 22:30
题目描述
给定圆的半径 3.0,输出 6.0000 18.8495 28.2743。
思路
直接套公式:直径 printf("%.4f") 保留 4 位小数即可。
参考代码
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-11-18 20:00
# update_at: 2026-11-18 20:00
import sys
PI = 3.14159 # 题面规定的圆周率取值,不要用 math.pi
def solve() -> None:
data = iter(map(float, sys.stdin.buffer.read().split()))
r = next(data) # 半径 0 < r <= 10000
diameter = 2 * r # 直径
circumference = 2 * PI * r # 周长 2πr
area = PI * r * r # 面积 πr²
# 三个量各出现一次,逐个命名后交给 f-string 统一保留 4 位小数
print(f"{diameter:.4f} {circumference:.4f} {area:.4f}")
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:读入圆的半径,按题面给定的圆周率输出直径、周长、面积,各保留 4 位小数。
#include <cstdio>
double r; // 圆的半径
int main() {
scanf("%lf", &r);
double pi = 3.14159; // 题面钦定的圆周率,不能用 math 的高精度常量
double diameter = 2 * r; // 直径 d = 2r
double circumference = 2 * pi * r; // 周长 C = 2πr
double area = pi * r * r; // 面积 S = πr²
printf("%.4f %.4f %.4f\n", diameter, circumference, area);
return 0;
}