The 17th Zhejiang University Programming Contest Sponsored by TuSimple A
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.
题意:换一瓶饮料需要a或者b种材料,现在我有x,y个材料,问最多换几个,如果是无限就输出INF
解法:模拟,设置一个上限,如果超过就输出INF
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long t; 4 long long a,b,c,d; 5 int main() 6 { 7 while(cin>>t) 8 { 9 while(t--) 10 { 11 cin>>a>>b>>c>>d; 12 long long an,bn; 13 long long sum=0; 14 long long x=0; 15 an=c; 16 bn=d; 17 long long y=0; 18 while(an>=a||bn>=b) 19 { 20 // cout<<an<<" "<<bn<<endl; 21 sum+=(an/a+bn/b); 22 long long an1=(an/a+bn/b)+an%a; 23 long long bn1=(an/a+bn/b)+bn%b; 24 if(an1>=an&&bn1>=bn) 25 { 26 x=1; 27 break; 28 } 29 else 30 { 31 an=an1; 32 bn=bn1; 33 } 34 y++; 35 if(y>=100) 36 { 37 x=1; 38 break; 39 } 40 } 41 if(x) 42 { 43 cout<<"INF"<<endl; 44 } 45 else 46 { 47 cout<<sum<<endl; 48 } 49 } 50 } 51 return 0; 52 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~