UVA 674 —— 簡單DP

674 - Coin Change

Time limit: 3.000 seconds

  Coin Change 

Suppose 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.


For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.


Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents.

Input 

The input file contains any number of lines, each one consisting of a number for the amount of money in cents.

Output 

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input 

11
26

Sample Output 

4
13



Miguel Revilla 
2000-08-14
題意是給你1,5,10,25,50各5種面值,問你一個數n能拆成多少種面值?
思路是一個遞推的DP,注意不要合起來累加,會造成重複。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>


using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))

#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 10000 + 5;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
int dp[MAXN];
int coin[5] = {1 , 5 , 10 , 25 , 50};
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    int n;
    dp[0] = 1;
    for(int k = 0 ; k < 5 ; k++)
    {
        for(int i = 0 ; i <= MAXN ; i++)
        {
            dp[i + coin[k]] += dp[i];
        }
    }

    while(~scanf("%d" , &n))
    {
        printf("%d\n" , dp[n]);
    }
    return 0;
}


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