您的位置:首页 > 其它

ZOJ1005 Jugs

2008-09-21 21:40 363 查看
这个题目出的不严谨,至少测试数据太弱了。可以每次先灌满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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: