1546. 零钱问题
描述
小明是一个销售员,客人在他的地方买了东西,付给了小明一定面值的钱之后,小明需要把多余的钱退给客人。客人付给了小明n,小明的东西的售价为m,小明能退回给客人的面额只能为[100,50,20,10,5,2,1]的组合。现在小明想要使纸币数量之和最小,请返回这个最小值。
1 <= m <= n <= 10000000001≤m≤n≤1000000000
您在真实的面试中是否遇到过这个题?
样例
Give n=100, m=29, return 3.
100-29=71
Ming retrieved one 50, one 20 and one 1.
So the answer is 3.
Give n=50, m=5, return 3.
50-5=45
Ming retrieved two 20 and one 5.
So the answer is 3.
public class Solution {
/**
* @param n: The guest paid
* @param m: the price
* @return: the sum of the number of banknotes
*/
public int coinProblem(int n, int m) {
// Write your code here
int mon = n - m;
int[] comb = {100, 50, 20, 10, 5, 2, 1};
int res = 0;
for (int i=0; i<comb.length; i++){
res += mon / comb[i];
mon = mon % comb[i];
}
return res;
}
}

浙公网安备 33010602011771号