Marjar Cola

来源:互联网 发布:苹果mac自有办公软件 编辑:程序博客网 时间:2024/05/20 12:24
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.

Input
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100), indicating the number of test cases. For each test case:

The first and only line contains four integers x, y, a, b (1 ≤ x, y, a, b ≤ 100). Their meanings are described above.

Output
For each test case, print one line containing one integer, indicating the number of bottles of cola Alice can drink. If Alice can drink an infinite number of bottles of cola, print "INF" (without the quotes) instead.

Sample Input
2
1 3 1 1
4 3 6 4
Sample Output
INF
4
Hint

For the second test case, Alice has 6 empty bottles and 4 bottle caps in hand. She can return 4 bottles and 3 caps to the company to get 2 full bottles of cola. Then she will have 4 empty bottles and 3 caps in hand. She can return them to the company again and get another 2 full bottles of cola. This time she has 2 bottles and 2 caps in hand, but they are not enough to make the exchange. So the answer is 4.


思路:有两种情况为无限   1.当瓶盖或空瓶数为1且持有的瓶盖数或空瓶数大于1, 2. 当瓶盖和空瓶数为2且持有的瓶盖数或空瓶数大于2时。


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){    int n,m,n1,m1,t;    scanf("%d",&t);    while(t--)    {        int sum=0;        scanf("%d %d %d %d",&n,&m,&n1,&m1);        if((n==1||m==1)&&(n1>=1||m1>=1)||(n==2&&m==2)&&(n1>=2||m1>=2))        {            printf("INF\n");        }        else        {            while(1)            {                if(n<=n1)                {                    n1-=n;                    n1++;                    m1++;                    sum++;                }                if(m<=m1)                {                    m1-=m;                    n1++;                    m1++;                    sum++;                }                if(n>n1&&m>m1)                    break;            }            printf("%d\n",sum);        }    }    return 0;}


原创粉丝点击