Enumeration algorithm 2-buy a hundred chickens for a hundred dollars

A hundred
dollars for a hundred chickens totals 3 yuan, a hen is 5 yuan, three chicks are 1 yuan, and a hundred yuan buys a hundred chickens. Please write the number of roosters, hens, and chickens.


【analysis】

Assuming that the number of roosters is cock, the number of hens is hen, and the number of chicks is chick, two relationships are obtained, cock+hen+chick=100, 3*cock+5*hen+chick/3=100

It is not difficult to get the range of cock [0,33] and the range of hen [0,20] by using the enumeration method.

code:
 

#include<stdio.h>
const int COCKPRICE = 3; /*一只公鸡的价格*/
const int HENPRICE = 5;  /*一只母鸡的价格*/
const int CHICKS = 3;   /*一元钱能买的小鸡数量*/
void Scheme(int money, int chooks);/*计算并输出购买方案*/
void main()
{
	int money = 100;/*钱的总数*/
	int chooks = 100;/*鸡的总数*/
	printf("购买方案如下:\n");
	Scheme(money, chooks);/*计算并输出购买方案*/
	getchar();
}
void Scheme(int money, int chooks)
/*计算并输出购买方案*/
{
	int maxCock = money / COCKPRICE;
	int maxHen = money / HENPRICE;
	int maxChick = chooks;
	int cock, hen, chick;
	for (cock = 0; cock < maxCock; ++cock)/*枚举公鸡的可能数量*/
	{
		for (hen = 0; hen < maxHen; hen++)/*枚举母鸡的可能数量*/
		{
			for (chick = 0; chick < maxChick; chick++)/*枚举小鸡的可能数量*/
			{
				/*约束条件*/
				if (0 == chick%CHICKS && cock + hen + chick == chooks
					&& COCKPRICE*cock + HENPRICE*hen + chick / CHICKS == money)
				{
					printf("公鸡: %2d, 母鸡: %2d, 小鸡: %2d\n", cock, hen, chick);
				}
			}
		}
	}
}

result:

 

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/104151938