zoukankan      html  css  js  c++  java
  • UVA-674 Coin Change---完全背包

    题目链接:

    https://vjudge.net/problem/UVA-674

    题目大意:

    有5种硬币, 面值分别为1、5、10、25、50,现在给出金额,问可以用多少种方式组成该面值。

    思路:

    每种硬币无限个,就是完全背包的题目,设dp[i][j]表示前i种纸币凑成价值为j的种数,

    状态转移方程就可推出dp[i][j] = dp[i - 1][j] + dp[i - 1][j - w[i]]

    初始化均为0,dp[0][0] = 1

     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<cstdio>
     7 #include<set>
     8 #include<map>
     9 #include<cmath>
    10 #include<sstream>
    11 using namespace std;
    12 typedef pair<int, int> Pair;
    13 typedef long long ll;
    14 const int INF = 0x3f3f3f3f;
    15 int T, n, m,d;
    16 const int maxn = 1e4 + 10;
    17 int a[] = {1,5,10,25,50};
    18 int dp[maxn];
    19 //dp[i][j]表示前i种货币凑成价值j的种数
    20 //dp[i][j] = dp[i - 1][j] + dp[i - 1][j - w[i]]
    21 int main()
    22 {
    23     while(cin >> n)
    24     {
    25         memset(dp, 0, sizeof(dp));
    26         dp[0] = 1;
    27         for(int i = 0; i < 5; i++)
    28         {
    29             for(int j = a[i]; j <= n; j++)//完全背包,正序
    30             {
    31                 dp[j] = dp[j] + dp[j - a[i]];
    32             }
    33         }
    34         cout<<dp[n]<<endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    159
    158
    157
    156
    155
    高中生都能看懂的莫比乌斯反演
    洛谷 P4449 于神之怒加强版
    洛谷 P3455 [POI2007]ZAP-Queries
    洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB
    Dirichlet卷积
  • 原文地址:https://www.cnblogs.com/fzl194/p/8824970.html
Copyright © 2011-2022 走看看