1005 Jugs

来源:互联网 发布:福建省来宝网络 编辑:程序博客网 时间:2024/06/13 23:46

直接用暴力求解法,每次都是将B中的水逐步的往A中倒,一直到B中的水的体积是我们所要求解的量即可。

// C++Exercise.cpp : 定义控制台应用程序的入口点。//#include<iostream>#include<string>#include<stack>using namespace std;int Ca, Cb, N;int main(){while (cin >> Ca >> Cb >> N){if (N == Ca){cout << "fill A" << endl << "success" << endl;}else if (N == Cb){cout << "fill B" << endl << "success" << endl;}else{cout <<"fill B" << endl << "pour B A" << endl;int temp = Cb - Ca;while (temp != N){cout << "empty A" << endl;if (temp < Ca){temp = Cb - (Ca - temp);cout << "pour B A" << endl << "fill B" << endl << "pour B A" << endl;}else{temp = temp - Ca;cout << "pour B A"<<endl;}}cout << "success" << endl;}}return 0;}