题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5
题目大意:
有两个容器A和B,A容器容量为a,B容器容量为b,容器没有刻度,问怎样才能在B容器里装下c容量的水。已知a与b互质,0<a<=b c<=b
解题思路:
本题突破口是a和b互质,可以找到一种一定可以构造出答案的方式解出来。不断的把往A容器放满,剩下的就是B能够盛出的容量。
代码:
//#include<CSpreadSheet.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int a,b,c;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d%d%d",&a,&b,&c))
{
int aa=0,bb=0; //分别表示A和B容器已经放的容量
while(bb!=c) //还没到要求
{
if(!bb) //把B容器放满
{
printf("fill B\n");
bb=b;
}
printf("pour B A\n"); //把B倒到A中
int temp=a-aa; //a中能够放的
if(bb>temp) //bb现有的水量可以把A倒满
{
bb-=temp; //剩下的
if(bb==c) //达到要求
break;
printf("empty A\n");
aa=0; //倒掉A容器里的
}
else
{
aa+=bb; //不足倒满的话 能倒多少是多少
printf("fill B\n"); //再把B倒满
bb=b;
}
}
printf("success\n");
}
return 0;
}