枚举三种情况,取个最小值就是答案。
第一种情况:每次只买单个竹鼠(a * x)。
第二种情况:买x / 3组3个竹鼠,买x % 3个组1个竹鼠(b * (x / 3) + a * (x % 3))。
第三种情况:买x / 3 + 1组3个竹鼠(b * (x / 3 + 1))。
注意中间计算会爆int,需要转成i64。
#include <iostream>
#include <vector>
#include <algorithm>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int a, b, x;
std::cin >> a >> b >> x;
auto cal = [&](int tar){
int cnt = tar / 3;
int rest = tar % 3;
return std::min({1LL * a * tar, 1LL * b * cnt + 1LL * a * rest, 1LL * b * (cnt + 1)});
};
std::cout << cal(x) << "\n";
return 0;
}

京公网安备 11010502036488号