USACO Section 2.3 Money Systems (整数划分完全背包思想)
2012-09-04 15:57
549 查看
Money Systems
The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.
The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.
Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).
The amount money to construct is N (1 <= N <= 10,000).
View Code
The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.
The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.
Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).
PROGRAM NAME: money
INPUT FORMAT
The number of coins in the system is V (1 <= V <= 25).The amount money to construct is N (1 <= N <= 10,000).
Line 1: | Two integers, V and N |
Lines 2..: | V integers that represent the available coins (no particular number of integers per line) |
SAMPLE INPUT (file money.in)
3 10 1 2 5
OUTPUT FORMAT
A single line containing the total number of ways to construct N money units using V coins.SAMPLE OUTPUT (file money.out)
10 题意:给你 n 种不同面值货币和面值为 v 的货币,用 n 种不同的面值的货币组成 v 有多少种组合。 分析:完全背包
View Code
/* ID: dizzy_l1 LANG: C++ TASK: money */ #include<iostream> #include<cstring> #include<cstdio> #define MAXN 30 #define MAXV 10010 using namespace std; long long d[MAXV]; int a[MAXN]; FILE *fin = fopen ("money.in", "r"); FILE *fout = fopen ("money.out", "w"); int main() { int n,v,i,j; while(fscanf(fin,"%d %d",&n,&v)==2) { memset(d,0,sizeof(d)); for(i=0;i<n;i++) fscanf(fin,"%d",&a[i]); d[0]=1; for(i=0;i<n;i++) { for(j=a[i];j<=v;j++) { d[j]+=d[j-a[i]]; } } fprintf(fout,"%lld\n",d[v]); } return 0; }
相关文章推荐
- USACO Section 2.2 Subset Sums(整数划分01背包思想)
- 2013-11-13 实验之整数划分(递归求解思想,条件判断输出)
- [背包问题] 01背包、完全背包、整数找零
- UVA - 10313 Pay the Price 整数的划分问题 01背包
- 整数划分问题(python)--递归 and 动态规划(m个盘里放n个苹果思想类似)
- 最小划分中体现的背包思想
- 【动态规划/背包】整数划分的5种情况
- Uva 11137 Ingenuous Cubrency(整数划分方案 背包)
- 完全背包问题----思想的理解
- USACO Section 3.1 Score Inflation(完全背包)
- USACO section 2.3 Money Systems(dp)
- USACO section 2.3 Money Systems(dp)
- poj3181【完全背包+整数拆分】
- 两个整数划分——一句话造成完全不同的算法
- USACO Section 2.3: Money Systems
- 动态规划(01背包、完全背包、多重部分和、LCS、LIS、划分数、多重集组合数)
- codeforces 543A Writing Code(dp降维优化,完全背包思想)
- USACO Section 2.3 The Longest Prefix - 一点点DP思想
- 51nod 1201[整数划分] 1259[整数划分V2] 1597 [有限背包计数问题]
- 算法篇-1-递归思想-整数划分&Fibonacci&Ackerman&Permutation&Hanoi