ZOJ-1005* 灌水问题

1005:给定两个不同容量的杯子A,B和目标水量,求倒水序列。设A<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;
}

}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值