按照特定规则实现javascript函数



🙋‍♂️我对javascript进行了评估在这里:

目标:

在中国文化中,在庆祝活动中给予";"红包";里面装着一点钱。大多数情况下,成年一代给予年轻一代。你想建立一个微信应用程序,帮助祖父母在孙辈之间分享他们的捐赠预算。

编写一个程序来计算";幸运礼物";(等于8(根据资金预算和赠与孙辈的数量

功能:

许多规则,混合了传统和迷信,构成了这份礼物的框架:

捐款不应包含金额4,因为这听起来像是";死的";捐款8英镑听起来是有利的"财富;如果不给其中一个孙辈您的算法必须返回等于8的捐赠数量,同时遵守以下规则:

花费全部预算(除非有足够的预算每个人8(不给4(按照传统,预算永远不会是4(不给0(除非预算不够(一旦遵守上述规则,最多得8分实现:

实现功能luckyMoney(金钱,礼物(其中:

将货币和礼物的整数作为输入:

0<钱<100

0<礼品<10

并返回等于8的捐赠数量作为整数

function luckyMoney(money,giftees) {
//write your code here
// to debug : console.error('Debug messages...");
return -1;
}

因此,我继续执行以下功能:

function luckyMoney(money,giftees){
if (money % 8 ===0){
return (money/8)
}else if(money%4===0){
return 0}
}

我认为我的代码是错误的

你觉得怎么样?

递归解决方案可能是最简单的。

function luckyMoney(money, giftees) {
if (money === 4) {
throw new Error("You cannot give 4");
}
if (money >= giftees * 8) {  // There is enough to give everyone 8
return giftees;
}
if (money < 8 + giftees - 1 || money === 12) { // There is not enough to give anyone 8, or giving someone 8 would mean the next person gets 4 (unlucky) or someone will get 0 (not allowed)
return 0;
}
// Otherwise, the number of eights must be one greater than if we gave (money - 8) to (giftees - 1)
return 1 + luckyMoney(money - 8, giftees - 1);
}
const testCases = [[8, 1, 1], [8, 2, 0], [12, 2, 0], [13, 2, 1], [13, 3, 1], [16, 2, 2],  [100, 10, 10], [100, 13, 11]];
for (const [money, giftees, expectedResult] of testCases) {
result = luckyMoney(money, giftees);
console.log(money, giftees, expectedResult, result);
}

或者,这里是一个带有循环的非递归版本。可能有一个非循环的纯数学解决方案会更简单,但我不确定它会是什么

function luckyMoney(money, giftees) {
if (money >= giftees * 8) {
return giftees;
}
let r = 0;
while (money >= 8 + giftees - r && money !== 12) {
r++;
money -= 8;
}
return r;
}

根据我对;"规格":
**剩余资金12的测试不完全正确。只有当你不得不在剩下的2个礼物中分享12个时,你才能再给一个8个。
示例:

  • 钱=12,礼物=3
    你可以用8个做1个信封,用2个做2个信封。没有人得到0,没有人得到4,最多得到8
  • money=28,giftees=5
    您可以制作3个有8个的信封和2个有2个的信封(也可以制作三个有8的信封,一个有3个有1个(

**用剩余资金进行测试是不准确的:必须使用r+1而不是r。以下是要测试的示例:

  • 钱=253,礼物=71
    你可以用8制作26个(超过25个(信封,用1制作45个信封
    26*8+45=208+45=253
    71-26=45

所以while测试应该是:

while (money >= 8 + giftees - (r+1) && (money !== 12 || giftees - r !==2 ) {

由于我们搜索最大值,我实现解决方案的方法是从上到下。以下是我的java实现(您必须将其转换为javascript…(:

if (money < 0) {
return -1;
}
if (giftees <= 0) {
return -1;
}
// fast return 0
if (money <= giftees || money < 8) {
return 0;
}
// dividing a signed integer by eight is same as shifting bits 3 times to the right,
int nbEightParts = money >> 3;
//fast return: is it possible to give 8 to all with that money ?
if (nbEightParts >= giftees)
return giftees;
// else start trying to give 8 to the maximum possible which is money/8
// multiplying a signed integer by eight is same as shifting bits 3 times to the left,
int remainingMoneyForOther = money - (nbEightParts << 3);
int remainingGiftees = giftees - nbEightParts;
// we must have enough money to give at least 1 to each,
// so we must ensure that after putting 8 in the max number
// of envelopes it remains at least 1 for all the other
// Otherwise, we have try with one less envelope.
while (remainingMoneyForOther < remainingGiftees) {
nbEightParts--;
remainingMoneyForOther = remainingMoneyForOther + 8;
remainingGiftees++;
}
// We must spend all money if we can't give 8 to everybody and avoid envelope with 4.
// We must decrease the number of envelopes that contains 8 if it remains 4 and only one giftee remaining, after giving 8 to all others.
if ((remainingGiftees == 1) && (remainingMoneyForOther == 4)) {
return --nbEightParts;
} else
return nbEightParts;

为了优化所完成的迭代次数;搜索";也可以这样做。

相关内容

  • 没有找到相关文章

最新更新