背包解决硬币问题专题

来源:互联网 发布:parse解析json字符串 编辑:程序博客网 时间:2024/06/06 04:15


link:http://acm.hdu.edu.cn/showproblem.php?pid=1284

钱币兑换问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6727    Accepted Submission(s): 3903


Problem Description
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
 

Input
每行只有一个正整数N,N小于32768。
 

Output
对应每个输入,输出兑换方法数。
 

Sample Input
293412553
 

Sample Output
71883113137761
 

Author
SmallBeer(CML)
 

Source
杭电ACM集训队训练赛(VII)
 

AC code:
#include<iostream>#include<stdio.h>#include<map>#include<vector>#include<set>#include<cstdlib>#include<string.h>#include<string>#include<algorithm>#include<cmath>#define MAXN 1000010#define LL long long#define EPS 1e-9using namespace std;int dp[MAXN],c[MAXN];int main(){    int n,i,j;    c[1]=1;    c[2]=2;    c[3]=3;    while(~scanf("%d",&n))    {        memset(dp,0,sizeof(dp));        dp[0]=1;        for(i=1;i<=3;i++)        {            for(j=c[i];j<=n;j++)            {                dp[j]+=dp[j-c[i]];            }        }        printf("%d\n",dp[n]);    }    return 0;}


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

Ignatius and the Princess III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14668    Accepted Submission(s): 10329


Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
 

Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
 

Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
 

Sample Input
41020
 

Sample Output
542627
 

Author
Ignatius.L
 

AC  code:

#include<iostream>#include<stdio.h>#include<map>#include<vector>#include<set>#include<cstdlib>#include<string.h>#include<string>#include<algorithm>#include<cmath>#define MAXN 1000010#define LL long long#define EPS 1e-9using namespace std;int dp[MAXN],c[MAXN];int main(){int n,i,j;for(i=1;i<=122;i++)c[i]=i;while(~scanf("%d",&n)){memset(dp,0,sizeof(dp));dp[0]=1;for(i=1;i<=n;i++){for(j=c[i];j<=n;j++){dp[j]+=dp[j-c[i]];}}printf("%d\n",dp[n]);}return 0;}


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


Square Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8983    Accepted Submission(s): 6135


Problem Description
People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland. 
There are four combinations of coins to pay ten credits: 

ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin. 

Your mission is to count the number of ways to pay a given amount using coins of Silverland.
 

Input
The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.
 

Output
For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output. 
 

Sample Input
210300
 

Sample Output
1427
 

Source
Asia 1999, Kyoto (Japan)
 

AC code:
#include<iostream>#include<stdio.h>#include<map>#include<vector>#include<set>#include<cstdlib>#include<string.h>#include<string>#include<algorithm>#include<cmath>#define MAXN 1000010#define LL long long#define EPS 1e-9using namespace std;int dp[MAXN],c[MAXN];int main(){int n,i,j;for(i=1;i<=17;i++)c[i]=i*i;while(~scanf("%d",&n)&&n){memset(dp,0,sizeof(dp));dp[0]=1;for(i=1;i<=17;i++){for(j=c[i];j<=n;j++){dp[j]+=dp[j-c[i]];}}printf("%d\n",dp[n]);}return 0;}



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): 14976    Accepted Submission(s): 5066


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.
 

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
1126
 

Sample Output
413
 

Author
Lily
 

Source
浙江工业大学网络选拔赛
 

注意:此题要求硬币总数不超过100

AC  code:
#include<iostream>#include<stdio.h>#include<map>#include<vector>#include<set>#include<cstdlib>#include<string.h>#include<string>#include<algorithm>#include<cmath>#define MAXN 1000010#define LL long long#define EPS 1e-9using namespace std;int dp[333][333],c[MAXN];LL ans;int main(){int n,i,j,k;c[1]=50;c[2]=25;c[3]=10;c[4]=5;c[5]=1;while(~scanf("%d",&n)){memset(dp,0,sizeof(dp));dp[0][0]=1;for(i=1;i<=5;i++){for(k=0;k<=100;k++){for(j=c[i];j<=n;j++)    {    dp[j][k]+=dp[j-c[i]][k-1];    }}}ans=0;for(i=0;i<=100;i++){ans+=dp[n][i];}printf("%d\n",ans);}return 0;}



0 0
原创粉丝点击