HDU 1284 錢幣兌換問題 完全揹包

傳送門:HDU 1284 錢幣兌換問題

分析:
本題 完全揹包 模版。詳情見註釋。

代碼如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define ms(x, y) memset(x, y, sizeof(x))
const double PI = acos(-1.0), eps = 1e-8;
int dp[32769],n,i,j;
int main() {
    dp[0] = 1;
    // 完全揹包打表
    for(int i=1; i<4; i++)
        for(int j=i; j<32769; j++)
            dp[j] += dp[j-i];  // 狀態轉移(dp[]數組在不同的遍歷階段進行了更新 1 2 3 種硬幣 正好對應了 1 2 3 分的價值)

    while(scanf("%d",&n) != EOF)
        printf("%d\n",dp[n]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章