`
linest
  • 浏览: 150241 次
  • 性别: Icon_minigender_1
  • 来自: 内蒙古
社区版块
存档分类
最新评论

ZOJ-1005* 灌水问题

    博客分类:
  • acm
 
阅读更多
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;
			}

		}
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics