我可以用给定数量的项目构造多少个对象
How many objects can I construct with given number of items
我有很多原料,我必须知道我能用它做的三明治(大三明治和小三明治)的最大数量。如果成分残留,则结果一定是错误的。
- 大三明治:4 片西红柿和 2 片奶酪
- 小三明治:2 片西红柿和 1 片奶酪
如果所有成分只能用于小三明治或最多一个大三明治,我的代码就可以工作。
如果成分剩余,它也会 returns false。
但是,如果我们需要一个以上的大三明治才能使用所有成分,则 returns 错误。
var result = []
var ispossible = function(tomatoes, cheese) {
/* we need in any case an even number of tomatoes slices */
if (tomatoes % 2 === 0) {
/* the easiest way is making as many small sandwiches as possible. The minimum number of big sandwiches is the number of cheese slices remaining when we devise the total number by 2 */
var bigSandwich = cheese % 2
var smallSandwichTomatoes = (tomatoes - 4 * bigSandwich) / 2
var smallSandwich = cheese - bigSandwich
console.log("we need" + smallSandwich + "small sandwiches and " + bigSandwich + "big sandwiches")
} else {
console.log("false, all the ingredients cannot be used")
}
}
ispossible(10, 3)
假设此解决方案可以帮助其他读者增强他们的基本算法、编程技能:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
说明
- 如果番茄片的数量不是'even'或者奶酪的数量不是番茄数量的一半,return 'false'.
- 确定'bigSandwiches'个西红柿的个数/4
- 使用剩余的西红柿片
确定'smallSandwiches'的数量
代码片段:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
[
[20, 10],
[8, 4],
[4, 3],
[14, 7]
].forEach(x => console.log('tomatoes: ' + x[0] + '\tcheese slices ' + x[1] + '\n' + bigSmallSandwiches(x[0], x[1])));
我有很多原料,我必须知道我能用它做的三明治(大三明治和小三明治)的最大数量。如果成分残留,则结果一定是错误的。
- 大三明治:4 片西红柿和 2 片奶酪
- 小三明治:2 片西红柿和 1 片奶酪
如果所有成分只能用于小三明治或最多一个大三明治,我的代码就可以工作。 如果成分剩余,它也会 returns false。 但是,如果我们需要一个以上的大三明治才能使用所有成分,则 returns 错误。
var result = []
var ispossible = function(tomatoes, cheese) {
/* we need in any case an even number of tomatoes slices */
if (tomatoes % 2 === 0) {
/* the easiest way is making as many small sandwiches as possible. The minimum number of big sandwiches is the number of cheese slices remaining when we devise the total number by 2 */
var bigSandwich = cheese % 2
var smallSandwichTomatoes = (tomatoes - 4 * bigSandwich) / 2
var smallSandwich = cheese - bigSandwich
console.log("we need" + smallSandwich + "small sandwiches and " + bigSandwich + "big sandwiches")
} else {
console.log("false, all the ingredients cannot be used")
}
}
ispossible(10, 3)
假设此解决方案可以帮助其他读者增强他们的基本算法、编程技能:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
说明
- 如果番茄片的数量不是'even'或者奶酪的数量不是番茄数量的一半,return 'false'.
- 确定'bigSandwiches'个西红柿的个数/4
- 使用剩余的西红柿片 确定'smallSandwiches'的数量
代码片段:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
[
[20, 10],
[8, 4],
[4, 3],
[14, 7]
].forEach(x => console.log('tomatoes: ' + x[0] + '\tcheese slices ' + x[1] + '\n' + bigSmallSandwiches(x[0], x[1])));