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 }

 

你可能感兴趣的:(change)