首页 > 代码库 > HDU2069 Coin Change 【暴力】
HDU2069 Coin Change 【暴力】
Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13500 Accepted Submission(s): 4510
Problem Description
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, or two 5-cent coins and one 1-cent coin, or 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 100 coins.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or 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 100 coins.
Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) 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
#include <stdio.h> int main() { int n, a, b, c, d, e, ans; while(scanf("%d", &n) == 1){ ans = 0; for(a = 0; a <= n/50; ++a) for(b = 0; b <= n/25; ++b) for(c = 0; c <= n/10; ++c) for(d = 0; d <= n/5; ++d) for(e = 0; e <= n; ++e) if(a*50 + b*25 + c*10 + d*5 + e == n && a+b+c+d+e <= 100) ++ans; printf("%d\n", ans); } return 0; }
提交代码:
#include <stdio.h> const int dp[] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 9, 9, 9, 9, 9, 13, 13, 13, 13, 13, 18, 18, 18, 18, 18, 24, 24, 24, 24, 24, 31, 31, 31, 31, 31, 39, 39, 39, 39, 39, 50, 50, 50, 50, 50, 62, 62, 62, 62, 62, 77, 77, 77, 77, 77, 93, 93, 93, 93, 93, 112, 112, 112, 112, 112, 134, 134, 134, 134, 134, 159, 159, 159, 159, 159, 187, 187, 187, 187, 187, 218, 218, 218, 218, 218, 252, 252, 252, 252, 252, 292, 291, 291, 291, 291, 333, 333, 333, 333, 332, 380, 380, 380, 379, 378, 430, 430, 429, 428, 427, 485, 484, 483, 482, 482, 544, 543, 542, 541, 539, 608, 607, 606, 604, 602, 677, 676, 673, 671, 670, 751, 748, 746, 744, 743, 828, 825, 823, 822, 818, 912, 910, 908, 904, 900, 1001, 999, 995, 990, 986, 1098, 1093, 1088, 1084, 1081, 1196, 1191, 1186, 1182, 1177, 1301, 1296, 1292, 1285, 1278, 1413, 1408, 1400, 1393, 1387, 1532, 1524, 1515, 1508, 1503, 1654, 1644, 1637, 1631, 1622, 1782, 1773, 1766, 1757, 1746, 1916, 1909, 1898, 1886, 1875, 2061, 2049, 2037, 2025, 2015, 2208, 2194, 2181, 2170, 2156, 2361, 2348, 2336, 2321, 2306, 2521, 2508, 2492, 2475, 2459, 2691, 2673, 2654, 2637, 2622, 2863, 2843, 2824, 2808, 2789, 3042, 3021, 3004, 2983, 2960, 3228, 3209, 3187, 3164, 3140, 3424, 3401, 3376, 3351, 3329, 3623, 3596, 3570, 3545, 3517, 3830}; int main() { int n; while(scanf("%d", &n) == 1) printf("%d\n", dp[n]); return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。