ZOJ 3948 Marjar Cola

Marjar Cola is on sale now! In order to attract more customers, Edward, the boss of Marjar Company, decides to launch a promotion: If a customer returns x empty cola bottles or y cola bottle caps to the company, he can get a full bottle of Marjar Cola for free!

Now, Alice has a empty cola bottles and b cola bottle caps, and she wants to drink as many bottles of cola as possible. Do you know how many full bottles of Marjar Cola she can drink?

Note that a bottle of cola consists of one cola bottle and one bottle cap.

x 个空可乐瓶或 y 个瓶盖可以换一瓶可乐(一瓶可乐包括一个可乐瓶以及一个瓶盖),已知有 a 个空瓶和 b 个瓶盖,问最多能换到多少瓶可乐?

解题思路

解法 1: 首先判断出无限喝可乐的情况,之后模拟空瓶(瓶盖)换可乐即可。

无限兑换的情况仅有 x=1 或 y=1 以及 x=y=2 & (a>=2 || b>=2) 。

解法 2: 鉴于上述无限兑换的情况可能容易想漏,故直接模拟也可以。由于 x, y, a, b 都较小,所以选择一个阈值作为上限,到达某个阈值即可视为他在无限兑换。

代码

#include<bits/stdc++.h>
using namespace std;
int x, y, a, b;
int solve()
{
    int cnt = 0;
    if(x == 1 || y == 1)    return -1;
    if(x == 2 && y == 2 && (a>=2 || b>=2))  return -1;
    while(a >= x || b >= y)
    {
        if(a >= x)  a-=x;
        else if(b >= y) b -= y;
        else    continue;
        a++,    b++,    cnt++;
    }
    return cnt;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d %d",&x, &y, &a,&b);
        int ans = solve();
        if(ans == -1)   printf("INF\n");
        else printf("%d\n", ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值