javascript - 找到所需金额的最小货币数量
问题描述
我做了一个小程序来找到所需数量的最少纸币(货币)。例如,假设我输入了一个金额 1121,并且我有以下这些值的数组: notes = [1, 2, 5, 10, 50, 100, 200, 500] 所以我的最终结果将是:
500 * 2(注)= 1000
100 * 1 = 100
20 * 1 = 20
1 * 1 = 1
那么总数将是1121。任何有助于理解的帮助将不胜感激。我知道它只需要一个 for 循环,但我在某些部分感到困惑。
这就是我所做的:https ://stackblitz.com/edit/angular-rufwzk?file=src%2Fapp%2Fapp.component.ts
解决方案
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');
}
}
这样做的诀窍是简单地记录所提到数量的音符数量。
推荐阅读
- c++ - 找出生成 N 对不发生冲突的唯一课程的方法的数量
- php - 如何在 html 区域停止 php 自动完成?
- kubernetes - Kubernetes 端点类型的生命周期
- javascript - Javascript,未捕获的 ReferenceError:未定义变量
- c# - 启用复选框以在 ASP.NET GridView 上可编辑,而无需单击编辑
- elasticsearch - 当查询文本为字母数字模式时,Elasticsearch 将返回所有记录
- asp.net - ASP.Net 2.0:提交后表单不重定向
- android - 无法从 firebase 检索数据(java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String)
- server - 尝试 ping 服务器时,我不断收到请求超时,这可能是我的错吗?
- android - 将 adb 输出分配给批处理脚本中的变量