Enumeration Solution Feed Ratios

Source: Internet
Author: User

  
   
 
    Feed Ratios 
    1998 ACM Finals, Dan Adkins
 

   
  

Farmer John feeds His cows only the finest mixture of cow food,which have 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-go tocombine them in integer multiples to form a mix with some go Al 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 integers units (the ' mixture ') of the first, second, and third mixture that Should is mixedtogether to achieve the goal ratio or print ' NONE '. ' Minimum number ' means the sum of the three non-negative mixture integers is minimized.

For the 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 is all non-negativeand smaller than in magnitude. The number of units of each type offeed in the mixture must is less than 100. The mixture ratios is notlinear combinations of each other.

Program Name:ratiosinput 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 51 2 33 7 12 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 the obtain the goal ratio. The Fourthnumber should is the multiple of the goal ratio obtained by mixing theinitial feed using the first three integer s as mixing ratios.

SAMPLE OUTPUT (file ratios.out)
8 1 5 7

Analysis

Because the amount of data is very small t (n) =10^6, so directly with the three-layer loop to enumerate all x, Y, z, and then check whether each combination is established.

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


Pass Code
   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]< C3/>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:c1033311lang: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;// Input data fscanf (FIN, "%d%d%d", &target[0],&target[1],&target[2]); for (i=0;i<3;++i) for (J=0;J&LT;3;++J) FSCANF (FIN, "%d", &feed[i][j]);//Extreme case int Sum=target[0]+target[1]+target[2];if (sum==0) {fprintf (Fout, "0 0 0 0\n"); Goto END;} Loop solver: I,j,k is 3 scale 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)//Find 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//output if (flag) fprintf (Fout, "%d%d%d%d\n", x,y,z,n), elsefprintf (Fout, "none\n"); End:return 0;}






Enumeration Solution Feed Ratios

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.