ZOJ1005 Jugs

68 篇文章 1 订阅
rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cdyk%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

这个题目出的不严谨,至少测试数据太弱了。可以每次先灌满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 ;
}
rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cdyk%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

代码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 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值