HDOJ-2069 Coin Change

这道题比比普通母函数题多了个限制就是总硬币数不能超过100,那么在记录每种方案时,同时要记录构成该种方案的硬币数.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>

using namespace std;

#define maxn 255 
int num[5] = {1, 5, 10, 25, 50};
vector<int> v1[maxn], v2[maxn]; 
int main(){

    //freopen("in.txt", "r", stdin); 
    int n;

    while(cin >> n){

        for(int i = 0; i <= n; i++){
           v1[i].clear();
           v2[i].clear();
        }
        v1[0].push_back(0);
        v2[0].push_back(0);

        for(int i = 0; i < 5; i++){
            for(int j = 0; j < n; j++){
                for(int h = 1; h <= 100 && h*num[i] + j <= n; h++){
                      for(int p = 0; p < v1[j].size(); p++){
                        if(v1[j][p] + h <= 100){
                          int m = h * num[i] + j;
                          v2[m].push_back(v1[j][p]+h);
                        }
                      } 
                }
            }
        for(int h = 0; h <= n; h++)
               v1[h] = v2[h];
        }
        cout << v1[n].size() << endl;
    }

    return 0;
} 

你可能感兴趣的:(HDOJ-2069 Coin Change)