uva 10717

来源:互联网 发布:ubuntu如何创建文件夹 编辑:程序博客网 时间:2024/06/06 18:22

题意:给出一些硬币的高度,和你要设计的椅子腿的高度,每个椅子的一条腿用不同的硬币来组装,问得到的椅子距离目标值最小的两种高度是多少(小于目标值,大于目标值)。

题解:遍历存储所有硬币的每4个组成一组,求出他们的最小公倍数,然后暴力找出距离目标值最小的两种值。


#include <stdio.h>#include <string.h>const int N = 55;const int INF = 0x3f3f3f3f;int c[N], h[N * N * N * N], h1[N * N * N * N];int lcm(int a, int b) {int m = a, n = b, c;while (b > 0) {c = a % b;a = b;b = c;}return m * n / a;}int main() {int n, t, table, m;while (scanf("%d%d", &n, &t) && n + t) {for (int i = 0; i < n; i++)scanf("%d", &c[i]);m = 0;for (int i = 0; i < n; i++)for (int j = i + 1; j < n; j++)for (int k = j + 1; k < n; k++)for (int l = k + 1; l < n; l++) {int a = lcm(c[i], c[j]);int b = lcm(a, c[k]);h[m++] = lcm(b, c[l]);}while (t--) {scanf("%d", &table);int minn = INF, maxx = INF;for (int i = 0; i < m; i++) {h1[i] = h[i];while (h1[i] < table)h1[i] += h[i];if (h1[i] == table) {minn = maxx = 0;break;}if (maxx > h1[i] - table)maxx = h1[i] - table;if (minn > table - h1[i] + h[i])minn = table - h1[i] + h[i];}printf("%d %d\n", table - minn, table + maxx);}}return 0;}


0 0
原创粉丝点击