usaco3.2.4 Feed Ratios

11 篇文章 0 订阅

一 原题

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: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 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: OK

Executing...
   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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值