uva674 Coin Change ——完全背包

August 15, 2013
Uva DP

link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=615 和完全背包一样的思想。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
int a[5]={1,5,10,25,50};
int f[7777];
int main(void)
{
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n;
    while (cin>>n) {
        memset(f,0,sizeof(f)); f[0]=1;
        for (int i = 0; i < 5; ++i) {
            for (int j=a[i]; j<=n;++j) {
                f[j]+=f[j-a[i]];
            }
        }
        cout<<f[n]<<endl;
    }
    return 0;
}

o(╯□╰)o认真理解一下这种思想就行了。不难

comments powered by Disqus