ZOJ1005 Jugs

这个题目出的不严谨,至少测试数据太弱了。可以每次先灌满A瓶,也可以每次都先灌满B瓶,反正题目都说了肯定有解,我感觉应该只能让最优解通过测试。

代码1

复制代码
#include <iostream>
using namespace std;

int main()
{
    
int nA,nB,n,rstA,rstB;
    
while (cin>>nA>>nB>>n)
    {
        rstA 
= 0;
        rstB 
= 0;
        
if (nB==n)
        {
            cout
<<"fill B"<<endl;
            cout
<<"success"<<endl;
            
continue;
        }
        
if (nA==n)
        {
            cout
<<"fill A"<<endl;
            cout
<<"pour A B"<<endl;
            cout
<<"success"<<endl;
            
continue;
        }
        
while (rstB!=n)
        {
            
if(rstA==0)
            {
                rstA
=nA;
                cout
<<"fill A"<<endl;
            }
            
if(rstB==nB)
            {
                rstB
=0;
                cout
<<"empty B"<<endl;
            }
            
if(rstA>(nB-rstB))
            {
//第二个容器中还有空,从第一个容器中取水将其灌满
                rstA-=(nB-rstB);//从第一个容器取水
                rstB=nB;//第二个容器满了
                cout<<"pour A B"<<endl;
            }
            
else
            {
//第一个容器中的水都倒入第二个
                rstB+=rstA;
                rstA
=0;
                cout
<<"pour A B"<<endl;
            }
        }
        cout
<<"success"<<endl;
    }
    
return 0;
}
复制代码

代码2

复制代码
#include <iostream>
using namespace std;

int main()
{
    
int nA,nB,n,rstA,rstB;
    
while (cin>>nA>>nB>>n)
    {
        rstA 
= 0;
        rstB 
= 0;
        
if (nB==n)
        {
            cout
<<"fill B"<<endl;
            cout
<<"success"<<endl;
            
continue;
        }
        
if (nA==n)
        {
            cout
<<"fill A"<<endl;
            cout
<<"pour A B"<<endl;
            cout
<<"success"<<endl;
            
continue;
        }
        
while (rstB!=n)
        {
            
if(rstB==0)
            {
                rstB
=nB;
                cout
<<"fill B"<<endl;
            }
            
if(rstA==nA)
            {
                rstA
=0;
                cout
<<"empty A"<<endl;
            }
            
if(rstB>(nA-rstA))
            {
//第一个容器中还有空,从第二个容器中取水将其灌满
                rstB-=(nA-rstA);//从第二个容器取水
                rstA=nA;//第一个容器满了
                cout<<"pour B A"<<endl;
            }
            
else
            {
//第二个容器中的水都倒入第一个
                rstA+=rstB;
                rstB
=0;
                cout
<<"pour B A"<<endl;
            }
        }
        cout
<<"success"<<endl;
    }
    
return 0;
}
复制代码

posted on   Phinecos(洞庭散人)  阅读(1279)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2007-09-21 java文件传输

导航

统计

点击右上角即可分享
微信分享提示