标签:算法 usaco c++ c feed ratios
Feed Ratios
1998 ACM Finals, Dan AdkinsFarmer 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:2your 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. |
3 4 5 1 2 3 3 7 1 2 1 2
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.
8 1 5 7
由于数据量非常小T(n)=10^6,所以直接用三层循环枚举出所有的x,y,z,再检查每种组合是否成立即可。
http://www.shangxueba.com/jingyan/1827405.html
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; }
标签:算法 usaco c++ c feed ratios
原文地址:http://blog.csdn.net/hiboy_111/article/details/43867861