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">
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 ;
}
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 ;
}