Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of representing n cents. (The result may be large, so you should return it modulo 1000000007)
Example1:
Input: n = 5 Output: 2 Explanation: There are two ways: 5=5 5=1+1+1+1+1 Example2:
Input: n = 10 Output: 4 Explanation: There are four ways: 10=10 10=5+5 10=5+1+1+1+1+1 10=1+1+1+1+1+1+1+1+1+1 Notes:
You can assume:
0 <= n <= 1000000
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/coin-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。、
class Solution {
private:
static constexpr int mod = 1000000007;
public:
int waysToChange(int n) {
int ans = 0;
for (int i = 0; i * 25 <= n; ++i) {
int rest = n - i * 25;
int a = rest / 10;
int b = rest % 10 / 5;
ans = (ans + (long long)(a + 1) * (a + b + 1) % mod) % mod;
}
return ans;
}
};
class Solution {
private:
static constexpr int mod = 1000000007;
static constexpr int coins[4] = {25, 10, 5, 1};
public:
int waysToChange(int n) {
vector<int> f(n + 1);
f[0] = 1;
for (int c = 0; c < 4; ++c) {
int coin = coins[c];
for (int i = coin; i <= n; ++i) {
f[i] = (f[i] + f[i - coin]) % mod;
}
}
return f[n];
}
};
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有