有人注意到Citi Prestige卡的做工非常烂吗?# Money - 海外理财
S*s
1 楼
是Facebook发的准备材料上的题目:
An atm can only dispense values of $1, $5, $20, and $50. Return the number
of unique ways that a $ amount of X can be tendered.
($1, $5) is distinct from ($5, $1)
Input: 4 Output: 1
Input: 6 Output: 3
Input: 100 Output: 954515231698
按照提示的解法,输入100的时候就溢出了。请问有什么办法可以避免?
private static Integer atm(Integer x) {
if (x <= 0)
return x == 0 ? 1 : 0;
return atm(x - 1) + atm(x - 5) + atm(x - 20) + atm(x - 100);
}
An atm can only dispense values of $1, $5, $20, and $50. Return the number
of unique ways that a $ amount of X can be tendered.
($1, $5) is distinct from ($5, $1)
Input: 4 Output: 1
Input: 6 Output: 3
Input: 100 Output: 954515231698
按照提示的解法,输入100的时候就溢出了。请问有什么办法可以避免?
private static Integer atm(Integer x) {
if (x <= 0)
return x == 0 ? 1 : 0;
return atm(x - 1) + atm(x - 5) + atm(x - 20) + atm(x - 100);
}