20 升水先换算成 20000 cm³,用圆柱体积 πr²h 一次除法向上取整即得最少桶数。
OJ: roj
题目 ID: 1032
难度:入门
标签:数学浮点数python
创建: 2026-09-29 15:03
更新: 2026-10-04 23:00
题目描述
大象要喝
输入一行两个整数
样例输入:23 11,样例输出:3。
思路
参考代码
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 15:03
# update_at: 2026-09-29 15:03
import math
import sys
THIRST = 20_000 # 大象要解渴需 20 升 = 20000 立方厘米(1 升 = 1000 cm³)
def bucket_count(depth: int, radius: int) -> int:
"""一桶水的立方厘米数能装满几桶才够 20000 cm³:向上取整。"""
volume = math.pi * radius * radius * depth # 圆柱体积 πr²h
return math.ceil(THIRST / volume)
def solve() -> None:
depth, radius = map(int, sys.stdin.buffer.read().split())
print(bucket_count(depth, radius))
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 23:00
* update_at: 2026-10-04 23:00
*/
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const double TARGET = 20000.0; // 20 升 = 20000 立方厘米
const double PI = acos(-1.0); // π
ll h, r; // 桶深、底面半径
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> r;
double volume = PI * r * r * h; // 每桶圆柱体积 πr²h
ll ans = ceil(TARGET / volume); // 至少喝够,向上取整
cout << ans << "\n";
return 0;
}