HDOJ-2069 Coin Change(母函数)

来源:互联网 发布:分析数据 编辑:程序博客网 时间:2024/05/21 10:00

这道题比比普通母函数题多了个限制就是总硬币数不能超过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;} 
0 0
原创粉丝点击