uva 357 Let Me Count The Ways (DP)

uva 357 Let Me Count The Ways

After making a purchase at a large department store, Mel‘s change was 17 cents. He received 1 dime, 1 nickel, and 2 pennies. Later that day, he was shopping at a convenience store. Again his change was 17 cents. This time he received 2 nickels and 7 pennies.
He began to wonder ‘ "How many stores can I shop in and receive 17 cents change in a different configuration of coins? After a suitable mental struggle, he decided the answer was 6. He then challenged you to consider the general problem.

Write a program which will determine the number of different combinations of US coins (penny: 1c, nickel: 5c, dime: 10c, quarter: 25c, half-dollar: 50c) which may be used to produce a given amount of money.

Input

The input will consist of a set of numbers between 0 and 30000 inclusive, one per line in the input file.

Output

The output will consist of the appropriate statement from the selection below on a single line in the output file for each input value. The number
m is the number your program computes, n is the input value.

There are mways to produce ncents change.

There is only 1 way to produce ncents change.

Sample input

17
11
4

Sample output

There are 6 ways to produce 17 cents change.
There are 4 ways to produce 11 cents change.
There is only 1 way to produce 4 cents change.

题目大意:给出一个金额数。要求求出用5种面值能够组成该金额数的方法数。

解题思路:注意输出时,当方法数为1时,要特判。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
typedef long long ll;
using namespace std;

int coin[5] = {1, 5, 10, 25, 50};
ll dp[30000];

int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		memset(dp, 0, sizeof(dp));
		dp[0] = 1;
		for (int i = 0; i < 5; i++) {
			for (int j = coin[i]; j <= n; j++) {
				if (dp[j - coin[i]]) {
					dp[j] += dp[j - coin[i]];
				}
			}
		}
		if (dp[n] == 1) {
			printf("There is only 1 way to produce %d cents change.\n", n);
		}
		else printf("There are %lld ways to produce %d cents change.\n", dp[n], n);
	}
	return 0;
}
时间: 2024-08-01 00:57:48

uva 357 Let Me Count The Ways (DP)的相关文章

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

UVA 357 Let Me Count The Ways(完全背包)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=293 题意: 有5种硬币: 1分 5分 10分 25分 和50分. 现在给你一个面值n, 问你有多少种方法能利用上述硬币组合出n分的金钱. 分析: 典型的完全背包问题. 本题的限制条件: 硬币钱数正好等于n 本题的目的条件: 求有多少种组合方法. 所以我们令dp[i][j]==x 表示用前i种

UVA 357 Let Me Count The Ways

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=293 Dynamic programming 注意overflow. 代码如下: 1 //============================================================================ 2 //

uva 10201 Adventures in Moving - Part IV (DP)

uva 10201 Adventures in Moving - Part IV 题目大意:借了一辆车,车里有100单位的油.要到达N米外的目的地(每走一米消耗一个单位的油),在这一段路程中,有若干个加油站,给出的数据是每个加油站的位置和加一单位油的价格.要求到达目的地且剩下100单位油的最小消费.(到达不了则输出Impossible) 解题思路:dp[i][j]数组代表的是第i个加油站油量为j的最小费用. 状态转移方程: dp[i][j]=min(dp[i][j],dp[i?1][j+(mil

hdu 1978 How many ways(dp)

Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标有的能量. 2.机器人只能向右或者向下走,并且每走一步消耗一单位能量. 3.机器人不能在原地停留. 4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量.如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点

HDU 1978 How many ways (DP)

一个比较简单的DP,希望读者在看题解之前再自己想想,争取自己AC . 这是一个计数问题,问你从(1,1)点到(n,m)点的行走方案数,因为还有一个能量问题,一开始我为了将状态表示完整,试着开三维数组记忆化搜索,用d[i][j][k]表示当前在i,j点还有k个能量的方法数,但是总得不到正确的答案,后来我发现这么做是不正确的,为什么呢?我们要明确一点,动态规划的特点是具有很多重叠子问题,大的最优解依赖于局部最优解,且每一个状态都具有相似的意义 . 请读者注意最后一句话,这很重要 . 我们先来看题目的

UVA - 357Let Me Count The Ways(完全背包)

题目:UVA - 357Let Me Count The Ways(完全背包) 题目大意:给出N,问用1, 5, 10, 25, 50,这些硬币能够凑出N的方式有多少种. 代码: #include <cstdio> #include <cstring> const int N = 5; const int maxn = 30005; const int coin[N] = {1, 5, 10, 25, 50}; typedef long long ll; ll f[maxn]; v

UVA 1025 A Spy in the Metro(DP)

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a s

【UVA】10285-Longest Run on a Snowboard(动态规划)

这题出简单了,不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 Longest Run on a Snowboard Accepted C++ 0.026 2014-08-07 11:43:51 枚举每个点进行遍历,使用记忆化搜索.要不会超时. #include<cstdio> #include<cstring> #include<iostream&