1005:给定两个不同容量的杯子A,B和目标水量,求倒水序列。设A<B
思路:一种简便方法,只要A为空就倒满A,将A倒向B,B溢出则倒空。直到符合要求。
思路:一种简便方法,只要A为空就倒满A,将A倒向B,B溢出则倒空。直到符合要求。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int A;
int B;
int target;
int inA;
int inB;
while(scanf("%d%d%d",&A,&B,&target)!=EOF)
{
inA=0;
inB=0;
while(1)
{
if(inA==0)
{
printf("fill A\n");
inA=A;
}
else
{
printf("pour A B\n");
inB+=inA;
if(inB>=B)
{
if(B!=target)
{
printf("empty B\n");
inA=inB-B;
inB=0;
}
else
inB=B;
}
else
{
inA=0;
}
}
if(inA==target||inB==target)
{
printf("success\n");
break;
}
}
}
}