hdu2069 (Coin change)

Source: Internet
Author: User
Tags cmath

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2069

Coin Change

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 23514 Accepted Submission (s): 8250


Problem Descriptionsuppose There is 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 had one cents, then we can make changes with one 10-cent coin and one 1-cent coin, or both 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 is four ways of making changes for one cents with the above coins. Note that we count this there is one of the making change for zero cent.

Write a program to find the total number of different ways of making changes for all amount of money in cents. Your program should is able to handle up to coins.

Inputthe input file contains any number of lines, each one consisting of a number (≤250) for the amount of money in cent S.

Outputfor each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input1126

Sample Output413

Authorlily

SOURCE Zhejiang University of Technology Network tryouts: There is a 50,25,10,5,1 cent of the coin, enter N, represent you want to make up the total number, ask you to use these coins just together into N, cut the total number of coins not more than 100 ideas: This super flood, the amount, vegetable chicken I thought very difficult, thought for a long time, Brains also did not want to come out, did not expect the direct 5 cycle is easy to solve, it is difficult to read the code it:
#include <iostream>#include<string.h>#include<map>#include<cstdio>#include<cstring>#include<stdio.h>#include<cmath>#include<ctype.h>#include<math.h>#include<algorithm>#include<Set>#include<queue>typedefLong Longll;using namespacestd;Constll mod=1e9;Const intmaxn= -+ -;Const intmaxm=1;Const intmaxx=1e4+Ten;Constll maxe= ++Ten;#defineINF 0x3f3f3f3f3f3f#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {        intans=0;  for(intI=0; i* -<=n;i++)        {             for(intj=0; j* -<=n;j++)            {                 for(intk=0; k*Ten<=n;k++)                {                     for(intL=0; l*5<=n;l++)                    {                         for(intm=0; m<=n;m++)                        {                            if(i* -+j* -+k*Ten+l*5+m==n&&i+j+k+l+m<= -) ans++; } }}} cout<<ans<<Endl; }    return 0;}

Thinking 2:DP thought, Dp[i][j] said that the value of I, with a J coins. The value of a coin can be saved with a[],dp[i][j]=dp[i][j]+dp[i-a[k]][j-1 .

See the code specifically:

#include <iostream>#include<string.h>#include<map>#include<cstdio>#include<cstring>#include<stdio.h>#include<cmath>#include<ctype.h>#include<math.h>#include<algorithm>#include<Set>#include<queue>typedefLong Longll;using namespacestd;Constll mod=1e9;Const intmaxn= -+ -;Const intmaxm=1;Const intmaxx=1e4+Ten;Constll maxe= ++Ten;#defineINF 0x3f3f3f3f3f3f#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1inta[5]={1,5,Ten, -, -};intdp[maxn][ the];//Dp[i][j] means that the value is I, using the number of J coinsintMain () {intN;  while(SCANF ("%d", &n)! =EOF) {memset (DP,0,sizeof(DP));//Initialize all to 0        intans=0; dp[0][0]=1;  for(intI=0;i<5; i++)//coins of 5 kinds of value        {             for(intj=1; j<= -; j + +)//number            {                 for(intk=a[i];k<=n;k++)//The minimum is the current a[i], which has been traversed to n{Dp[k][j]+=dp[k-a[i]][j-1];//from the previous plus use this one case                }            }        }         for(intI=0; i<= -; i++) ans+=Dp[n][i]; cout<<ans<<Endl; }    return 0;}

hdu2069 (Coin change)

Related Article

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.