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,如需转载请自行联系原作者
目录
相关文章
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
122 0
|
人工智能 BI 应用服务中间件
|
机器学习/深度学习
|
人工智能 机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions t...
1113 0
|
人工智能 BI JavaScript
POJ 2260(ZOJ 1949) Error Correction 一个水题
Description A boolean matrix has the parity property when each row and each column has an even sum, i.
1119 0