背包解决硬币问题专题

171 篇文章 0 订阅
102 篇文章 0 订阅


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
  
  
2934 12553
 

Sample Output
  
  
718831 13137761
 

Author
SmallBeer(CML)
 

Source
 

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-9
using 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;
}


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
   
   
4 10 20
 

Sample Output
   
   
5 42 627
 

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-9
using 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
  
  
2 10 30 0
 

Sample Output
  
  
1 4 27
 

Source
 

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-9
using 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;
}




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
   
   
11 26
 

Sample Output
   
   
4 13
 

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-9
using 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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值