2014
01-22

# UVa-147-Dollars(钱币)-[组合数学]

Time limit: 3.000 seconds

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

0.20
2.00
0.00

## Sample Output输出示例

0.20                4
2.00              293

## Solution解答

#include <iomanip>
#include <iostream>
using namespace std;
int main(void) {
//钱币数，全部除以5
int Coins[] = {1, 2, 4, 10, 20, 40, 100, 200, 400, 1000, 2000};
long long Tbl[6001] = {1}; //各金额组合方案数总表
//以下开始建表，按钱币种类进行循环
for (int i = 0; i < 11; i++){
//仅使用i之前的钱币进行组合，但不包括不使用i的情况
for (int j = Coins[i]; j < 6001; j++) {
//先减掉第i种钱币，然后加上剩下的金额的组合方案数
Tbl[j] += Tbl[j - Coins[i]];
}
} //以下为设定格式，查表输出结果。
cout << fixed << showpoint << setprecision(2);
for (float fIn; cin >> fIn && fIn != 0; cout << endl) {
cout << setw(6) << fIn << setw(17) << Tbl[(int)(fIn * 20 + 0.5f)];
}
return 0;
}

1. 不同的编译器，printf(),执行的顺序不同，所以结果也就不同，建议不要那么写