HackerRank# The Coin Change Problem

原题地址

 

背包问题,没啥好说的,记得用long long,否则会爆

 

代码:

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <cstring>
 7 using namespace std;
 8 
 9 #define MAX_N 256
10 #define MAX_M 64
11 
12 long long f[MAX_N];
13 int c[MAX_M];
14 int m, n;
15 
16 int main() {
17     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
18     cin >> n >> m;
19     for (int i = 0; i < m; i++)
20         cin >> c[i];
21 
22     memset(f, 0, sizeof(f));
23     f[0] = 1;
24     for (int i = m - 1; i >= 0; i--)
25         for (int j = 1; j <= n; j++)
26             if (j >= c[i])
27                 f[j] += f[j - c[i]];
28 
29     cout << f[n] << endl;
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/boring09/p/4467692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值