一 原题

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 knows the precise mixture of these easily mixable grains, he can not buy that mixture! He buys three other mixtures of the three grains and then combines them to form the perfect mixture.

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

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

        1:2:33:7:12:1:2

your program should find some minimum number of integer units (the `mixture') of the first, second, and third mixture that should be mixed together 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, one unit of mixture 2, and five units of mixture 3 to get seven units of the 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-negative and smaller than 100 in magnitude. The number of units of each type of feed in the mixture must be less than 100. The mixture ratios are not linear combinations of each other.

PROGRAM NAME: ratios

INPUT FORMAT

Line 1: Three space separated integers that represent the goal ratios
Line 2..4: Each contain three space separated integers that represent the ratios of the three mixtures purchased.

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 or the word `NONE'. The first three integers should represent the number of units of each mixture to use to obtain the goal ratio. The fourth number should be the multiple of the goal ratio obtained by mixing the initial feed using the first three integers as mixing ratios.

SAMPLE OUTPUT (file ratios.out)

8 1 5 7

二 分析

题目里说了,线性组合的系数小于100。然后就枚举三个系数就完了。。有个小坑:可能有原料需求量为0,然后代码就丑的不能看了。

三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: ratios
LANG: C++Compiling...
Compile: OKExecuting...Test 1: TEST OK [0.000 secs, 4184 KB]Test 2: TEST OK [0.011 secs, 4184 KB]Test 3: TEST OK [0.000 secs, 4184 KB]Test 4: TEST OK [0.043 secs, 4184 KB]Test 5: TEST OK [0.032 secs, 4184 KB]Test 6: TEST OK [0.022 secs, 4184 KB]All tests OK.

Your program ('ratios') produced all correct answers! This is your submission #2 for this problem. Congratulations!

AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:ratios
*/#include<cstdio>
#include<vector>
using namespace std;struct Ratio {int comp[3];
}a[4];int f(int n1, int n2, int n3) {if(n1 == 0 && n2 == 0 && n3 == 0) return -1;int sum[3] = {0};bool zero[3] = {0};vector<int> v;for(int i = 0; i < 3; i++) {sum[i] += n1 * a[1].comp[i] +n2 * a[2].comp[i] +n3 * a[3].comp[i];}for(int i = 0; i < 3; i++) {if(a[0].comp[i] == 0) {zero[i] = true;}}for(int i = 0; i < 3; i++) {if(zero[i]) {if(sum[i] != 0) return -1;}  else {if(sum[i] % a[0].comp[i] != 0) return -1;v.push_back(sum[i] / a[0].comp[i]);}}for(int i = 0; i < v.size(); i++) {for(int j = i + 1; j < v.size(); j++) {if(v[i] != v[j]) return -1;}}return v[0];
}int main() {freopen("ratios.in", "r", stdin);freopen("ratios.out", "w", stdout);for(int i = 0; i < 4; i++) {for(int j = 0; j < 3; j++) {scanf("%d", &a[i].comp[j]);}}bool has_ans = false;for(int i = 0; i < 100; i++) {if(has_ans) break;for(int j = 0; j < 100; j++) {if(has_ans) break;for(int k = 0; k < 100; k++) {int tmp = f(i, j, k);if(tmp != -1) {printf("%d %d %d %d\n", i, j, k, tmp);has_ans = true;break;}}}}if(!has_ans) printf("NONE\n");return 0;
}

usaco3.2.4 Feed Ratios相关推荐

  1. 【例题】【高斯消元】USACO3.2.4 Feed Ratios

    NKOJ1828 [USACO3.2.4]Feed Ratios饲料调配 时间限制 : 10000 MS 空间限制 : 65536 KB 问题描述 农夫约翰从来只用调配得最好的饲料来喂他的奶牛.饲料用 ...

  2. USACO3.2.4 Feed Ratios (ratios)

    其实就是求一个最简配比a,b,c,t使a(x1,y1,z1)+b(x2,y2,z2)+c(x3,y3,z3)=t(k1,k2,k3) 用行列式求出a,b,c,t.如果t=0或abct不同号则说明无解. ...

  3. usaco Feed Ratios

    一开始没看清100以内的限制没法下手百度后发现他们都说100以内回去又把题读了读.....理解能力有点问题... 数字很小就简单了枚举...注意0注意0注意0.我被坑死了开始写好一个没考虑0,wa了发 ...

  4. Feed Ratios

    题意:有三种饲料,饲料中均含有大麦.燕麦和小麦这三种成分,并且知道每种饲料中三种成分的比例.求按何比例混合三种饲料可以使大麦.燕麦和小麦的比例为x:y:z 解题思路: 根据输入列出4元线性方程组 应用 ...

  5. 【USACO题库】3.2.4 Feed Ratios饲料调配

    这一题,有许多的细节要注意一下!特别是循环中的判断!先看一下代码: #include<cstdio> using namespace std; int a[4][4]; int main( ...

  6. USACO-Section 3.2 Feed Ratios(枚举)

    此处有目录↑ 描述 农夫约翰从来只用调配得最好的饲料来喂他的奶牛.饲料用三种原料调配成:大麦,燕麦和小麦.他知道自己的饲料精确的配比,在市场上是买不到这样的饲料的.他只好购买其他三种混合饲料(同样都由 ...

  7. USACO Training Section 3.2 Feed Ratios

    [url="http://ace.delos.com/usacoprob2?a=JbTv7diNbha&S=ratios"]英文原题[/url] [url="ht ...

  8. [usaco3.2.4]ratios

    题目传送门:http://www.nocow.cn/index.php/Translate:USACO/ratios 这道题也是直接枚举... /* ID:abc31261 LANG:C++ TASK ...

  9. USACO--3.2Feed Ratios

    WF上的水题,直接模拟即可.注意为0情况的处理,我写的有点复杂了. 代码如下: /* ID: 15674811 LANG: C++ TASK: ratios */#include<iostrea ...

最新文章

  1. 检测硬盘使用时长_如何检测硬盘问题
  2. 华为上半年手机销量_十月京东手机销量!华为mate40火爆,苹果11近百万销量
  3. python使用退格键时出现^H解决方法
  4. 深夜,你的手机为谁开?
  5. navicat连接CentOS的mysql在创建存储过程时出现1146 - Table ‘mysql.proc’ doesn't exist的问题
  6. 数据库备份DBS 新增Region支持:华北2、华东2和华南1
  7. python内容限制_Python 限制线程的最大数量的方法(Semaphore)
  8. Zookeeper UI管理界面安装
  9. python编程快速上手 让繁琐工作自动化 豆瓣_2019年,这些豆瓣评分9.0以上的8本程序员好书你都知道吗?...
  10. webservice学习总结(一)-- WebService相关概念介绍
  11. php多表条件排除然后输出,php – 从具有多个要求的两个表中选择值,但仅使用其中一个...
  12. css 这个特性,你敢信
  13. 电脑连接西门子S7-200CPU的步骤
  14. c语言utc时间转换,gps时转换为utc时间方法
  15. SMOTE算法(处理非平衡数据)
  16. 蓝牙耳机连接笔记本声音卡顿解决办法
  17. 南信大 计算机与软件学院 校花,美哭了!南京12所高校最美“校花”新鲜出炉!颜值爆表!你的母校上榜了吗?...
  18. 纸飞机飞行曲线matlab,从小到大只会做个纸飞机?关于折纸的「高端」技巧通通告诉你...
  19. python数据化运营案例---简单销售预测案例(线性回归模型)
  20. muti-thread fork

热门文章

  1. python计算矩阵行列式_基础 | Python 下的行列式值
  2. Android --- 5G网络,android系统开发教程
  3. VBA 根据股票代码查询价格
  4. 【信息系统项目管理师】第三章 立项管理思维导图
  5. LeetCode 全站第一,牛逼!
  6. windows registry = control pannel
  7. [渝粤教育] 盐城师范学院 高等代数 参考 资料
  8. LoadLibraryEx(DONT_RESOLVE_DLL_REFERENCES)的缺陷
  9. 【GD32F310开发板试用】Contiki-NG在GD32F310的移植
  10. 5G/NR 标识详解之5G-GUTI