Jugs_1005

这个问题很简单。主要就是两种情况 iMax*i - iMin*j或者 iMin*j - iMax*(i-1)

由于对于打印方案的时候,结束判定错误,导致了两次错误提交。成绩不理想啊

#include <stdio.h>

int CalcHCF(int iMax, int iMin)
{
	int m = iMax % iMin;
	while(m != 0)
	{
		iMax = iMin;
		iMin = m;
		m = iMax % iMin;
	}
	return iMin;
}

int CalcLCM(int iMax, int iMin)
{
	return iMax * iMin / CalcHCF(iMax, iMin);
}

void PrintTheMethod(int x, int y, int res)
{
	int iMax, iMin;
	char cMax, cMin;
	x > y ? (iMax = x, cMax = 'A', iMin = y, cMin = 'B') : (iMax = y, cMax = 'B', iMin = x, cMin = 'A');
	int lcm = CalcLCM(iMax, iMin);
	int iMaxLen = lcm / iMax;
	int iMaxIndex = 1, iMinIndex = 1;

	bool bFind = false;
	char iSolution;			// 1: 使用iMax*i - iMin*j 方案 2:使用iMin*j - iMax*i方案
	for (; iMaxIndex <= iMaxLen; iMaxIndex++)
	{
		for (; iMinIndex*iMin < iMaxIndex*iMax; iMinIndex++)
		{
			if((iMax*iMaxIndex - iMinIndex*iMin) == res)
			{
				iSolution = 1;
				bFind = true;
				break;
			}
			else if((iMinIndex*iMin - iMax*(iMaxIndex-1)) == res)
			{
				iSolution = 2;
				bFind = true;
				break;
			}
		}

		if (bFind)
		{
			break;
		}
	}

	//if (bFind)
	//{
	// 第一方案
	// 1. fill Max 2. pour Max min if max > min then 3 else if got minindex then finished else 2 3. empty min then 2
	if(iSolution == 1)
	{
		bool bFinished = false;
		int iMaxNow = 1;
		printf("fill %c\n", cMax);
		for (int iMinNow = 1; iMinNow <= iMinIndex; iMinNow++)
		{
			printf("pour %c %c\n", cMax, cMin);
			if(iMinNow * iMin >= iMaxNow*iMax)
			{
				iMaxNow++;
				printf("fill %c\n", cMax);
				printf("pour %c %c\n", cMax, cMin);
			}
			printf("empty %c\n", cMin);
		}
	}
	// 第二方案
	// 1.fill min 2. pour min max until max ful then 3 or got minidex then finished else 1 3.empty max 4. pour min max then 1
	else
	{
		int iMaxNow = 1;
		for (int  iMinNow = 1; iMinNow <= iMinIndex; iMinNow++)
		{
			printf("fill %c\n", cMin);
			printf("pour %c %c\n", cMin, cMax);		
			if(iMinNow*iMin > iMaxNow*iMax)
			{
				iMaxNow++;
				printf("empty %c\n", cMax);
				printf("pour %c %c\n", cMin, cMax);
			}
		}	
	}
	//}

	printf("success\n");
}

int main()
{
	int x, y, res;
	while (scanf("%d %d %d", &x, &y, &res) != EOF)
	{
		PrintTheMethod(x, y, res);
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值