147 - Dollars
有面值给定的钱币,给定一个钱数,求出总共有多少种方法表示这个钱数,题目说给定的钱数都是5c的倍数,所以可以把钱都除以5,这里我将得到的钱数都乘上了100,就将浮点数变成了整数,更加容易处理.dp[i][j]表示钱i划分最大钱数为第j种的划分数,转移方程为dp[i][j] = dp[i-mp[j][k](0<=k<=j).在写的时候第二维数组开小了1,出现了十分怪异的错误,找了好久.另外就是注意输出格式.
代码如下:
/*************************************************************************
> File Name: 147.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年11月12日 星期三 21时57分55秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#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 6010
//这里第二维设成了10,越界了,产生了奇怪的错误。
ll dp[N][20], ans[N], res[N];
int cnt = 11;
//钱数都是5的倍数,所以除以了5
int mp[] = {1, 2, 4, 10, 20, 40, 100, 200, 400, 1000, 2000};
char str[10];
int main(int argc, char *argv[])
{
//二维数组递推
dp[0][0] = 1;
for (int i = 1; i < N; ++i) {
for (int j = 0; j < cnt; ++j) {
dp[i][j] = 0;
if (i - mp[j] >= 0) {
for (int k = 0; k <= j; ++k) {
dp[i][j] += dp[i - mp[j]][k];
}
}
}
}
for (int i = 0; i < N; ++i) {
ans[i] = 0;
for (int j = 0; j < cnt; ++j) {
ans[i] += dp[i][j];
}
}
//一维数组递推
clr(res, 0);
res[0] = 1;
for (int i = 0; i < cnt; ++i) {
for (int j = mp[i]; j < N; ++j) {
res[j] += res[j - mp[i]];
}
}
while (scanf("%s", str) != EOF) {
if (strcmp(str, "0.00") == 0) {
break;
}
int n = 0;
int len = strlen(str);
for (int i = 0; i < len; ++i) {
if (str[i] != '.') {
n = n * 10 + str[i] - '0';
}
}
n /= 5;
//printf("%6s%17lld\n", str, ans[n]);
printf("%6s%17lld\n", str, res[n]);
}
return 0;
}
/*
New Zealand currency consists of $100, $50, $20, $10, and $5 notes and
$2, $1, 50c, 20c, 10c and 5c coins. Write a program that will determine,
for any given amount, in how many ways that amount may be made up.
Changing the order of listing does not increase the count. Thus 20c may
be made up in 4 ways.
Input
Input will consist of a series of real numbers no greater than
$300.00 each on a separate line. Each amount will be valid, that is
will be a multiple of 5c. The file will be terminated by a line containing
zero (0.00).
Output
Output will consist of a line for each of the amounts in the input, each
line consisting of the amount of money (with two decimal places and right
justified in a field of width 6), followed by the number of ways in which
that amount may be made up, right justified in a field of width 17.
Sample input
0.20
2.00
0.00
Sample output
0.20 4
2.00 293
*/