题解 | 清楚姐姐买竹鼠

清楚姐姐买竹鼠

https://www.nowcoder.com/practice/816dfe69f83042108cf8c74531a94a35

枚举三种情况,取个最小值就是答案。

第一种情况:每次只买单个竹鼠(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;
}

全部评论

相关推荐

04-03 09:32
已编辑
华南农业大学 golang
我的代码出BUG了:"晚点发个邮件调整一下时间",你收到新的邮件没,如果没有收到新的邮件,那就需要进入面试链接留痕,否则系统会判定你迟到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务