Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找所需金额的最小货币数_Javascript_Angular_Typescript_For Loop_Stackblitz - Fatal编程技术网

Javascript 查找所需金额的最小货币数

Javascript 查找所需金额的最小货币数,javascript,angular,typescript,for-loop,stackblitz,Javascript,Angular,Typescript,For Loop,Stackblitz,我做了一个小程序,找到所需金额的最小纸币数量(货币)。例如,假设我输入了一个金额1121,下面是这些值的数组: 注意=[1,2,5,10,50,100,200,500]所以我的最终结果是: 500*2(注)=1000 100*1=100 20*1=20 1*1=1 那么总数将是1121。任何有助于理解的帮助都将不胜感激。我知道这只需要一个循环,但我对某些部分感到困惑 以下是我所做的:以下是您所需的正确代码。我希望我做对了 requiredNotes(amount) { let not

我做了一个小程序,找到所需金额的最小纸币数量(货币)。例如,假设我输入了一个金额1121,下面是这些值的数组: 注意=[1,2,5,10,50,100,200,500]所以我的最终结果是:

500*2(注)=1000

100*1=100

20*1=20

1*1=1

那么总数将是1121。任何有助于理解的帮助都将不胜感激。我知道这只需要一个循环,但我对某些部分感到困惑


以下是我所做的:

以下是您所需的正确代码。我希望我做对了

  requiredNotes(amount) {
    let noteArry = this.notes,
      quotient,
      remainder,
      temp,
      noteCount = 0,
      eachNote,
      remainingAmount = 0;
    for (let i = noteArry.length - 1; i >= 0; i--) {
      if (amount >= noteArry[i]) {
        quotient = Math.floor(amount / noteArry[i]);
        remainder = amount % noteArry[i];
        amount = amount - (noteArry[i] * quotient);
        if (amount == 0) {
          console.log("note:-", noteArry[i], ",number of note", quotient);
          break;
        } else if (amount != 0) {
          console.log("note:-", noteArry[i], ",number of note", quotient);
        }
      } else {
                continue;
      }
    }
  }

这样做的诀窍是只需注销所述金额的注释数。

我简化了一点您的代码(使用JS Map):


您的代码有什么问题?我不确定问题是什么…你混淆了哪些部分?我想存储每个值的数量及其商,以检查它是否等于输入的数量:1121数组=[1,2,5,10,50,100,200,500];从反面开始1)1121/500=500*2=1000,余数121和商2)121/200不可能3)121/100=100*1=100,余数21和商1 4)21/50不可能5)21/10=10*2=20,余数1和商2 6)1/5不可能我的最终结果是哪种货币纸币:500,100,10每种货币的数字:2,1,2(商)@user184994我的问题是我想安慰每一种货币和它重复的数字。将数组视为货币数组。e、 g输入了1000,那么我需要500卢比的钞票2次。@confusedGuy请在你的帖子中输入所有的解释。是的,准确无误。这太简单了。现在我觉得自己很愚蠢。非常感谢。我真的被困在了保留挂载部分。是的,我被困在了将保留挂载与原始挂载进行比较。现在看来很简单。
for (let i = noteArry.length - 1; i >= 0; i--) {
  if (amount >= noteArry[i]) {
    quotient = Math.floor(amount / noteArry[i]);
    remainder = amount % noteArry[i];
    remainingAmount = noteArry[i] * quotient;
    amount=amount-remainingAmount;
    console.log('number of notes =', noteArry[i], 'x', quotient,' notes');
  }
}
...
notesMap = new Map();
...

requiredNotes(amount) {
  for (let i = this.notes.length - 1; i >= 0 && amount; i--) {
    const qty = Math.floor(amount / this.notes[i]);
    qty && this.notesMap.set(this.notes[i], qty);
    amount = amount % this.notes[i];
  }

  const entries = Array.from(this.notesMap.entries());
  this.requireNotes = entries.map(([curr, qty]) => `${curr} * ${qty} = ${curr * qty}`);
}