枚举解Feed Ratios

 
 

Feed Ratios

1998 ACM Finals, Dan Adkins

Farmer John feeds his cows only the finest mixture of cow food,which has three components: Barley, Oats, and Wheat. While he knowsthe precise mixture of these easily mixable grains, he can not buy thatmixture! He buys three other mixtures of the three grains and thencombines them to form the perfect mixture.

Given a set of integer ratios barley:oats:wheat, find a way tocombine them IN INTEGER MULTIPLES to form a mix with some goal ratiox:y:z.

For example, given the goal 3:4:5 and the ratios of three mixtures:

        1:2:3
        3:7:1
        2:1:2
your program should find some minimum number of integer units (the`mixture') of the first, second, and third mixture that should be mixedtogether to achieve the goal ratio or print `NONE'. `Minimum number'means the sum of the three non-negative mixture integers is minimized.

For this example, you can combine eight units of mixture 1, oneunit of mixture 2, and five units of mixture 3 to get seven units ofthe goal ratio:

    8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5)

Integers in the goal ratio and mixture ratios are all non-negativeand smaller than 100 in magnitude. The number of units of each type offeed in the mixture must be less than 100. The mixture ratios are notlinear combinations of each other.

PROGRAM NAME: ratios

INPUT FORMAT

Line 1:Three space separated integers thatrepresent the goal ratios
Line 2..4:Each contain three spaceseparated integers that represent the ratios of the three mixturespurchased.

SAMPLE INPUT (file ratios.in)

3 4 5
1 2 3
3 7 1
2 1 2

OUTPUT FORMAT

The output file should contain one line containing four integers orthe word `NONE'. The first three integers should represent the numberof units of each mixture to use to obtain the goal ratio. The fourthnumber should be the multiple of the goal ratio obtained by mixing theinitial feed using the first three integers as mixing ratios.

SAMPLE OUTPUT (file ratios.out)

8 1 5 7

分析

由于数据量非常小T(n)=10^6,所以直接用三层循环枚举出所有的x,y,z,再检查每种组合是否成立即可。

http://www.shangxueba.com/jingyan/1827405.html


pass代码

Executing...
   Test 1: TEST OK [0.011 secs, 3500 KB]
   Test 2: TEST OK [0.011 secs, 3500 KB]
   Test 3: TEST OK [0.016 secs, 3500 KB]
   Test 4: TEST OK [0.016 secs, 3500 KB]
   Test 5: TEST OK [0.014 secs, 3500 KB]
   Test 6: TEST OK [0.016 secs, 3500 KB]

All tests OK.

/*
ID: c1033311
LANG: C++
TASK: ratios
*/

#include<stdio.h>

int main(){
	FILE *fin=fopen("ratios.in","r");
	FILE *fout=fopen("ratios.out","w");

	int feed[3][3],target[3];
	int i,j,x,y,z,n;
	int max=1000,X,Y,Z,N;
	bool flag=false;



	//输入数据
	fscanf(fin,"%d%d%d",&target[0],&target[1],&target[2]);
	
	for(i=0;i<3;++i)
		for(j=0;j<3;++j)
			fscanf(fin,"%d",&feed[i][j]);



	//极端情况
	int sum=target[0]+target[1]+target[2];
	if(sum==0)  
	{
		fprintf(fout,"0 0 0 0\n");
		goto end;
	}



	//循环求解:i,j,k为3个比例
	for(x=0;x<100;++x)
		for(y=0;y<100;++y)
			for(z=0;z<100;++z)
			{
				int a,b,c;
				a=x*feed[0][0]+y*feed[1][0]+z*feed[2][0];
				b=x*feed[0][1]+y*feed[1][1]+z*feed[2][1];
				c=x*feed[0][2]+y*feed[1][2]+z*feed[2][2];
				
				if((a+b+c)%sum==0) //求出n
				{
					n=(a+b+c)/sum;

					if(n!=0 && a==n*target[0] && b==n*target[1] && c==n*target[2] && max>x+y+z)
					{
						max=x+y+z;
						X=x;
						Y=y;
						Z=z;
						N=n;
						flag=true;
					}
				}

			}//end for



	//输出结果
	if(flag)
		fprintf(fout,"%d %d %d %d\n",X,Y,Z,N);
	else
		fprintf(fout,"NONE\n");
		

end:	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值