UVA - 674 Coin Change (完全揹包)

題意

有5種硬幣,分別價值1,5,10,25,50;
問取的硬幣總價值爲n有幾種取法;

思路

常規的完全揹包問題

代碼

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
int cost[5]= {1,5,10,25,50};
int dp[100000];
int main()
{
	//預處理出所有結果
    memset(dp,0,sizeof dp);
    dp[0]=1;
    for(int i=0; i<5; i++)
        for(int j=0; j<=7489-cost[i]; j++)
            dp[j+cost[i]]+=dp[j];
	
    int n;
    while(~scanf("%d",&n))
        printf("%d\n",dp[n]);
        
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章