USACO 2.3.4 Dynamic Planning of Money Systems

Source: Internet
Author: User

Money SystemsThe 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 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 other 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 (C/C ++) D Int64 (Free Pascal ). program name: moneyINPUT FORMATThe 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 NLines 2 ..: V integers that represent the available coins (no participant number of integers per line) sample input (file money. in) 3 101 2 5 output formata single line containing the total number of ways to constru Ct N money units using V coins. sample output (file money. out) 10/* Second */return to the dynamic planning. This is a classic coin change problem. I use a two-dimensional array (I remember another one-dimensional array method with better space performance ). The basic idea is to use only the first j coins to find the I yuan. Now we want to record how many methods are available to ensure that the j coins are used. So how can we ensure that the j coins will be used? It is to subtract the face value of the j-coin from the I-Yuan coin, and then see how many methods I-j yuan can be used to find out the j-coin. In particular, if I-j is 0, we need to add 1, which means we have directly found the coin. Therefore, we use a two-dimensional array method [I] [j] to record the use of the first j coins and ensure that the first j coins are used, find out how much I yuan is available. I also want to say that I don't know why. At first, my program running results on my own computer were different from those running on USACO. Then I expanded the size of the two-dimensional array from 10000 to 10003. Program: [cpp]/* ID: zhaorui3 PROG: money LANG: C ++ */# include <fstream> using namespace std; int kinds [26] = {0 }; long methods [10010] [26] ={{ 0 }}; int main () {ifstream fin ("money. in "); ofstream fout (" money. out "); int v, n; fin> v> n; for (int I = 0; I <v; ++ I) fin> kinds [I]; for (int I = 1; I <= n; I ++) {for (int j = 0; j <v; j ++) {if (I <kinds [j]) continue; for (int k = 0; k <= j; ++ K) {www.2cto.com if (I-kinds [j]! = 0) methods [I] [j] + = methods [I-kinds [j] [k];} if (I-kinds [j] = 0) methods [I] [j] ++ ;}} long out = 0; for (int I = 0; I <v; ++ I) out + = methods [n] [I]; fout <out <endl; fin. close (); fout. close (); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.