// 表示用4种(1,2,5,10)硬币计算凑齐n块钱有多少种方法,i表示出的10块钱的总额,例如 // i==0的时候,就表示要用3种硬币凑齐所有的n块钱 publicstaticlongget4n(int n){ long sum = 0; for (int i = 0; i <= n; i += 10) { sum += get3n(n - i); } return sum; }
// 表示用3种(1,2,5)硬币计算凑齐n块钱有多少种方法 privatestaticlongget3n(int n){ long sum = 0; for (int i = 0; i <= n; i += 5) { sum += get2n(n - i); } return sum; }
// 出的10块钱的总额不得大于 maxPer publicstaticlongget4n(int n, int maxPer){ long sum = 0; for (int i = 0; i <= maxPer; i += 10) { sum += get3n(n - i, maxPer); } return sum; }
// 出的5块钱的总额不得大于 maxPer privatestaticlongget3n(int n, int maxPer){ long sum = 0; for (int i = 0; i <= maxPer; i += 5) { sum += get2n(n - i, maxPer); } return sum; }
privatestaticlongget2n(int n, int maxPer){ if (n < maxPer) { return1 + (n / 2); } else { int bMax = maxPer / 2 - 1; int bMin = (n - maxPer) / 2 + 1; if (bMax < bMin) { return0; } return bMax - bMin + 1; } }