枚举解Feed Ratios
来源:互联网 发布:在淘宝上买苹果6可靠吗 编辑:程序博客网 时间:2024/06/05 14:37
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 ratiosLine 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 2OUTPUT 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: 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;//输入数据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);elsefprintf(fout,"NONE\n");end:return 0;}
- 枚举解Feed Ratios
- [U]3.2.4 Feed Ratios 枚举
- USACO-Section 3.2 Feed Ratios(枚举)
- Feed Ratios
- Feed Ratios
- USACO / Feed Ratios (枚举||克莱姆法则||高斯消元)
- USACO 3.2 Feed Ratios (ratios)
- USACO3.2.4 Feed Ratios (ratios)
- USACO3.2.4 Feed Ratios (ratios)
- 【搜索】【USACO】Feed Ratios
- [USACO3.2.4 Feed Ratios]
- USACO--3.2Feed Ratios
- usaco Feed Ratios
- usaco3.2.4 Feed Ratios
- USACO 3.2.4 feed ratios
- USACO Feed Ratios, Magic Squares
- USACO 3.2.4 Feed Ratios
- USACO Feed Ratios 解题报告
- LeetCode Remove Duplicates from Sorted Array
- UVA 674 Coin Change
- 对象存储学习小结
- UVa 506 - System Dependencies
- POJ 2406 Power Strings(KMP)
- 枚举解Feed Ratios
- UVA 10003 Cutting Sticks
- Codeforces Beta Round #5 E. Bindian Signalizing
- iOS 报错 setValue:forUndefinedKey:]: this class is not key value coding
- poj 1068
- 接口
- HDU 1087 Super Jumping! Jumping! Jumping!
- 一切成功源于积累——20150217 欧美 美加 涨无穷 跌无尽
- CUDA Cuts: Fast Graph Cuts on the GPU