uva 674 Coin Change【揹包】

Brief Description:

there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

n元找零,求找法。


Analyse:

揹包問題


CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=10007;

int c[]={0,1,5,10,25,50};
int dp[N];

inline int MAX(int a,int b)
{
    return a>b?a:b;
}

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        dp[0]=1;
        for(int i=1;i<=n;i++) dp[i]=0;
        for(int i=1;i<=5;i++)
        {
            
            for(int j=c[i];j<=n;j++)
            {
                dp[j]+=dp[j-c[i]];
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}




發佈了149 篇原創文章 · 獲贊 16 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章