首页 > 试题广场 >

清楚姐姐买竹鼠

[编程题]清楚姐姐买竹鼠
  • 热度指数:6039 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}清楚姐姐途经山村,遇到一家售卖竹鼠的商铺:
\hspace{23pt}\bullet\, 花费 a 元可购买 1 只竹鼠;
\hspace{23pt}\bullet\, 花费 b 元可购买 3 只竹鼠。

\hspace{15pt}给定 a,b,x,求买到至少 x 只竹鼠所需的最小花费。

输入描述:
\hspace{15pt}在一行上输入三个整数 a,b,x\left(1\leqq a,b,x\leqq 10^9\right)


输出描述:
\hspace{15pt}输出一个整数,表示最少需花费的金额。
示例1

输入

4 10 10

输出

34

说明

\,\,\,\,\,\,\,\,\,\,花费 3\times b = 30 元买 9 只竹鼠,再花费 1\times a = 4 元买 1 只竹鼠,共花费 34 元。我们可以证明,没有更优的购买方式。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here

    while ((line = await readline())) {
        let [a, b, x] = line.split(" ").map((item) => BigInt(item));
        // console.log(a, b, x)
        if (b >= 3n * a) {
            console.log((x * a).toString());
        } else {
            // const m = Math.floor(x / 3n);
            const m = x / 3n;
            const r = x % 3n;
            const candidate1 = m * b + r * a;
            const candidate2 = (m + 1n) * b;

            // 比较两个 BigInt,返回较小的
            if (candidate1 < candidate2) {
                console.log(candidate1.toString());
            } else {
                console.log(candidate2.toString());
            }
        }
    }
})();

发表于 2025-10-06 18:53:47 回复(0)