码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank# The Coin Change Problem

时间:2015-04-30 00:48:03      阅读:231      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!