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




本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/09/21/1295472.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值