首页 > 试题广场 >

清楚姐姐买竹鼠

[编程题]清楚姐姐买竹鼠
  • 热度指数: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 元。我们可以证明,没有更优的购买方式。
特么买三只还有可能比买一只便宜
发表于 2025-11-12 15:20:41 回复(0)
53 46 97 预期输出是不是错了
发表于 2025-08-09 14:31:43 回复(2)
六百六十六,买一只比买三只还要贵,演都不演了
发表于 2025-12-01 20:31:46 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();  // 1只竹鼠的价格
        long b = scanner.nextLong();  // 3只竹鼠的价格
        long x = scanner.nextLong();  // 需要的竹鼠数量
        
        // 计算3只一组的单价是否比单只购买更划算
        // 如果3只的价格比单买3只便宜,则优先考虑3只一组的购买方式
        if (b < 3 * a) {
            // 计算需要多少组3只
            long groups = x / 3;
            // 计算剩余不足3只的数量
            long remainder = x % 3;
            
            // 总花费 = 组数*每组价格 + 剩余数量的最小花费
            // 剩余数量的花费可以选择单买,或者再买一组3只(可能更便宜)
            long cost = groups * b + Math.min(remainder * a, b);
            System.out.println(cost);
        } else {
            // 如果3只一组不划算,全部单买
            System.out.println(x * a);
        }
    }
}
    

发表于 2025-09-03 14:10:02 回复(0)
用C的坑点,有表示范围,比如double 只能表示到16位 ,long double 表示到19位
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define N 100005
//坑点:22161931 259253696 707723857
//double 类型的表示只能到16位数,我们要用19位的long double来表示一只
int a,b,x;
int ans=0;
signed main() {
    cin>>a>>b>>x;
    long double yi=a,er=b/3.0;
    //平均下来看看哪一种购买的一只便宜
    if(yi>er){
        ans=x/3*b+min((int)((x%3)*yi),b);
    }
    else {
        ans=yi*x;
    }

    cout<<ans<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")


发表于 2025-08-24 11:20:42 回复(0)
53一只竹鼠,46三只竹鼠,这商铺是真把姐姐当日本人坑啊
发表于 2025-07-31 20:23:43 回复(1)
/*● 比较3a和 b:
如果3a<b:单买3只要3a元,比套装b元便宜→单买划算
如果3a>b:单买3只要3a元,比套装b元贵→套装划算
如果3a=b: 价格一样

*/
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    long long a, b, x;
    cin >> a >> b >> x;
   
    long long k = x / 3;      // 最多完整套装数
    long long r = x % 3;      // 剩余只数
   
    // 总是考虑三种方案
    long long ans1 = x * a;               // 全部单买
    long long ans2 = k * b + r * a;       // 混合:k套 + r只单
    long long ans3 = (k + 1) * b;         // 全套装(向上取整)
   
    long long ans = min({ans1, ans2, ans3});
   
    cout << ans << endl;
   
    return 0;
}
发表于 2025-12-04 21:55:31 回复(0)
#include <iostream>

int main()
{
    int a = 0,b = 0,x = 0;
    std::cin>>a>>b>>x;

    long long num = x;
    long long tar = std::min(3LL,num);

    long long ans = 0;
    while(a * tar > b)
    {
        ans += b;
        num-=3;
        tar = std::min(3LL,num);
    }
    if(num > 0)
    {
        ans += num * a;
    }
    std::cout<<ans<<'\n';
}

发表于 2025-11-29 18:38:09 回复(0)
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)
我觉得应该直接比较单价。
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int x = in.nextInt();

        if(b/3 < a){
        int y = x %3;
        int z = (x-y)/3;
        int re = z*b + a;
        System.out.println(re);
        }else{
        System.out.println(a*x);
        }
    }
}

发表于 2025-09-28 15:27:50 回复(0)
import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        long a = ad(); // a元买1只
        long b = ad(); // b元买3只
        long x = ad(); // x只
        // 如果3a>b,说明你卖的东西贵了

        // ans = (x % 3) * a + (x / 3) * b;
        long ans = 0;
        // 9 + 1
        if (a >= b) { // 用b买划算
            if (x % 3 == 0) { // 特判 %1和%2的情况
                ans = (x / 3) * b;
            } else {
                ans = (x / 3 + 1) * b;
            }
        } else if (3 * a < b) { // 用a划算
            ans = x * a;
        } else { // a和b自由组合
            ans = (x % 3) * a + (x / 3) * b;
        }
        System.out.println(ans);

    }

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static long ad() throws IOException {
        st.nextToken();
        return (long) st.nval;
    }
}

发表于 2025-09-08 15:56:53 回复(0)
import java.util.*;
import java.io.*;

// 能买三只时,计算a,b两种策略买三只花费选最小的
// 不能买三只时,选取a策略购买和b策略最小值,选b时多买几只也没事儿,因为题目有至少
public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] line = bf.readLine().split(" ");
        long a = Integer.parseInt(line[0]);//a*3会超过int范围,所以a用long
        int b = Integer.parseInt(line[1]);
        int x = Integer.parseInt(line[2]);

        long sumMin = 0;//花费用long
        while (x >0) {
            if (x >= 3) {
                sumMin += Math.min(a * 3, b);
            } else {
                sumMin += Math.min(a*x, b);
            }
            x-=3;
        }
        System.out.println(sumMin);
    }
}

发表于 2025-09-02 11:05:11 回复(0)
a,b,x = map(int,input().split())
print(min(b*(x//3)+a*(x-3*(x//3)),a*x,b*(x//3+1)))
发表于 2025-09-02 04:08:02 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        long b = in.nextLong();
        long x = in.nextLong();

        if ( b / 3 < a) {
            long cost = x % 3;
            if (cost == 0) {
                System.out.println((x / 3) * b);
            } else if (cost * a < b){
                System.out.println((x / 3) * b + cost * a);
            }else {
                System.out.println((x / 3) * b + b);
            }
        } else {
            System.out.println(a * x);
        }
    }

发表于 2025-08-26 15:33:58 回复(0)
a, b, x = map(int , input().split())

cost = 0
if b > 3 * a:
    cost = x * a
elif b < a:
    cost = b * (x // 3 + x % 3)
else:
    cost = b * (x // 3) + a * (x % 3)

print(cost)

发表于 2025-08-14 21:55:25 回复(0)
a, b, x = map(int, input().split())
print(min(x // 3 * b + a * (x % 3), a * x))
发表于 2025-08-14 16:07:49 回复(0)