ZOJ 3948 Marjar Cola

来源:互联网 发布:知乎 陈茂辉 编辑:程序博客网 时间:2024/06/06 04:09

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
原创粉丝点击