uvaoj 674 - Coin Change

求找零钱方式的种类数,刚开始想的是一维的状态,但发现一维的状态太少,会导致重复,就用了二维状态。dp[i][j]表示i元钱找出的零钱数最大是第j种零钱的种数。
这样dp[i][j]就等于dp[i-type[j]][k]的和,其中k为0到j,type[j]是第j中零钱的元数,显然i-type[j]要大于0,另外有一点需要注意的是,初始情况dp[0][0] = 1,相当于有0元时,最大找钱数为0的情况有一种。先递推出所有的情况,存起来,最后O1的询问就得到答案了。
因为dp[i][j]依赖的是左上角行元素的和,所以就可以使用滚动数组直接求和,滚动钱币种数就行了.
代码如下:
/*************************************************************************
	> File Name: 674.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月28日 星期二 22时49分24秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 10000

ll dp[N][10], n, res[N];
ll cnt = 5;
ll type[] = {1, 5, 10, 25, 50};

int main(int argc, char *argv[])
{
	clr(dp, 0);
	//特殊情况
	dp[0][0] = 1;
	for (int i = 1; i < N; ++i) {
		for (int j = 0; j < cnt; ++j) {
			if (i - type[j] >= 0) {
				for (int k = 0; k <= j; ++k) {
					dp[i][j] += dp[i - type[j]][k];
				}
			}
		}
	}

	//可以写成滚动数组
	//因为前边dp[i][j]依赖的是它左上角的行元素的和,可以直接
	//将他们求和,滚动钱币种类次数
	clr(res, 0);
	res[0] = 1;
	for (int i = 0; i < cnt; ++i) {
		for (int j = type[i]; j < N; ++j) {
			res[j] += res[j - type[i]];
		}
	}

	while (cin >> n) {
		ll ans = 0;
		for (int i = 0; i < cnt; ++i) {
			ans += dp[n][i];
		}

		//cout << ans << endl;
		cout << res[n] << endl;
	}

	return 0;
}

/*
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
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值