题目如标题,我对这道理的解题大致为:
一种思路,两种算法。
思路如下:
三个不同面值的钱,1是最小值,所以这道题目的最优解是一块钱有0张或者1张。
两种算法
x:7元的张数
y:5元的张数
- 1:列出两个式子:7x+5y=81 和 7x+5y=80,可以通过这两者关系,进行求整计算,得出的结果取最小值。
- 2:因为 7>5,所以只要(81-12*n) 或 (80-12*n) 的结果能够被7整除即为最优。
算法
第一种解法:
function get() {
let max = Math.floor(81 / 5);
let arrs = [];
for (let i = 1; i < max; i++) {
let x1 = (81 - 5 * i) / 7;
let x2 = (80 - 5 * i) / 7;
if (x1 % 1 === 0) {
arrs.push({
x: x1,
y: i,
z: 0
});
}
if (x2 % 1 === 0) {
arrs.push({
x: x2,
y: i,
z: 1
});
}
}
return arrs;
}
结果为:
第二种解法:
function get() {
let max = Math.floor(81 / 12);
let arrs = [];
for (let i = 1; i <= max; i++) {
let n1 = 81 - i * 12;
let n2 = 80 - i * 12;
if (n1 % 7 === 0) {
arrs.push({
x: i + n1 / 7,
y: i,
z: 0
});
}
if (n2 % 7 === 0) {
arrs.push({
x: i + n2 / 7,
y: i,
z: 1
});
}
}
return arrs;
}
结果为: