【算法】【动态规划】Coin Exchange
2015-08-28 10:42
225 查看
算法描述:
Korean coins consist of 6 levels of 1 won, 5 won, 10 won, 50 won, 100 won, and 500 won. If you make 256 won change, you need 5 coins: two coins of 100 won, one coin of 50 won, one coin of 5 won, and one coin of1 won.
If you make 8 won change in a country that coin unit consists of three levels of 1 won, 4 won and 6 won, you can form one coin of 6 won & two coins of 1 won, and two coins of 4 won. In the first case, you need total 3 coins, and
in the second case, you will need 2 coins in total. If the target is to make coins by the minimum number of coins, you should choose the second case. When coin units & wanted changes are given, find out the required minimum number to make change. The number
of coins you have is infinite.
Time Limit: 1 seconds for 10 cases. (java 2 seconds) (If your program exceeds this time limit, the answers that have been already printed are ignored and the score becomes 0. So, it may be better to print a wrong answer when
a specific test case might cause your program to exceed the time limit. One guide for the time limit excess would be the size of the input.)
[Input]
Several test cases can be included in the inputs. C, the number of cases is given in the first row of the inputs. After that, the test cases as many as C (1 ≤ C ≤ 10) are given in a row.
Kinds of coins, N are given on the first row per each test case. (1 ≤ N ≤ 10)
Units of each coin, M are given separately with a blank on the second row. (1 ≤ M ≤ W)
Changes to give, W are given on the third row. (1 ≤ W ≤ 64000)
[Output]
For the T-th test case, “case #T” should be printed out in the first line, followed by an integer which is
output the minimum number of coins. If it is not possible to make change, output ‘impossible’.
[I/O Example]
Input
2 ← There are two test cases.
3 ← Case #1
1 4 6
8
6 ← Case #2
1 4 5 7 16 20
4758
Output
Case #1
2
Case #2
240
分析:
动态规划源码:
/* You should use the statndard input/output in order to receive a score properly. Do not use file input and output Please be very careful. */ #include <iostream> #include <cstring> using namespace std; int N; int Won; int Coins[10]; int main(int argc, char** argv) { int T, test_case; /* The freopen function below opens input.txt file in read only mode, and afterward, the program will read from input.txt file instead of standard(keyboard) input. To test your program, you may save input data in input.txt file, and use freopen function to read from the file when using cin function. You may remove the comment symbols(//) in the below statement and use it. Use #include<cstdio> or #include <stdio.h> to use the function in your program. But before submission, you must remove the freopen function or rewrite comment symbols(//). */ freopen("input.txt", "r", stdin); cin >> T; for (test_case = 0; test_case < T; test_case++) { ///////////////////////////////////////////////////////////////////////////////////////////// /* Implement your algorithm here. The answer to the case will be stored in variable Answer. */ ///////////////////////////////////////////////////////////////////////////////////////////// int i, j; Won = 0; memset(Coins, 0x00, sizeof(Coins)); cin >> N; for (i = 0; i < N; i++) { cin >> Coins[i]; } cin >> Won; int *coinNum = new int[Won + 1]; coinNum[0] = 0; for (i = 1; i <= Won; i++) { int minNum = i; for (j = 0; j < N; j++) { if (i >= Coins[j]) { if (coinNum[i - Coins[j]] + 1 <= minNum) { minNum = coinNum[i - Coins[j]] + 1; } } } coinNum[i] = minNum; } // Print the answer to standard output(screen). cout << "Case #" << test_case + 1 << endl; cout << coinNum[Won] << endl; delete[]coinNum; } return 0;//Your program should return 0 on normal termination. }
相关文章推荐
- ZOJ 1649 Rescue
- php 函数func_get_args()、func_get_arg()与func_num_args()之间的比较
- SQL语句中,为什么where子句不能使用列别名,而order by却可以?
- cocos2d-x 使用lua实现像素点击检测
- iOS_Json字符串转字典、字典转成Json字符串
- jQuery实现动态添加和删除一个div
- lucene高级搜索
- [LeetCode] 20 - Valid Parentheses
- Android中的Selector的用法
- oracle contains函数 全文检索
- Samba及其基本应用
- vs2010 error c2679 binary
- linux shell——命令之乐
- iOS开发-OC-类、对象、方法
- Django之第一个app<6>
- 叶存菜鸟setter与getter应用笔记
- 逊哥dp专题 总结(普通dp,斜率优化dp,数位dp)
- VS2010 Web网站发布详解
- 怎样让Mac成为一个有效的生产力工具
- 新手如何成长一点点思考