首页 > 其他 > 详细

HackerRank# The Coin Change Problem

时间:2015-04-30 00:47:16      阅读:164      评论:0      收藏:0      [点我收藏+]

原题地址

 

背包问题,没啥好说的,记得用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 }

 

HackerRank# The Coin Change Problem

原文:http://www.cnblogs.com/boring09/p/4467692.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!