大象喝水查

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

20 升水先换算成 20000 cm³,用圆柱体积 πr²h 一次除法向上取整即得最少桶数。

OJ: roj

题目 ID: 1032

难度:入门

标签:数学浮点数python

创建: 2026-09-29 15:03

更新: 2026-10-04 23:00

题目描述

大象要喝 2020 升水解渴,只有一个深 hh 厘米、底面半径 rr 厘米的小圆桶。每桶装满,问至少要喝多少桶。

输入一行两个整数 h,rh, r,输出一个整数表示最少桶数。

样例输入:23 11,样例输出:3。

思路

2020 升 = 20000 cm320000\ \text{cm}^3,每桶容积 V=πr2hV = \pi r^2 h,答案即 ⌈20000/V⌉\lceil 20000 / V \rceil。注意必须向上取整,向下取整会不够喝。

参考代码

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;
}