hdu2069Coin Change(暴力求解----动态规划(背包)求解---搜索--)

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11533    Accepted Submission(s): 3845


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

 

 


 

//用C写0ms,java 170ms,注意for循环的结束判定的剪枝,( 很暴力)

 

import java.util.Scanner;

public class hdu2069coin_change {
  
	public static void main(String[] args) {
		// TODO Auto-generated method stub
         Scanner sc= new Scanner (System.in);
         int a,b,c,d,e,n;
         int[]s=new int[251];
     	for(n=0;n<=250;n++)
      		for(e=0;50*e<=n;e++)
      			 for(d=0;25*d<=n-50*e;d++)
      				  for(c=0;10*c<=n-50*e-25*d;c++)
      				        for(b=0;5*b<=n-50*e-25*d-10*c;b++){
                                                 a=n-50*e-25*d-10*c-5*b;
         						if(a+b+c+d+e<=100)
         							s[n]++;
      				       }
      			
        while(sc.hasNext()){
        	 int m=sc.nextInt();           
             
               System.out.println(s[m]);
        }     		 
    }
}         


 

用动态规划求解:

我们手中有50、25、10、5、1五种面额的硬币。然后要用这些硬币来表示j块钱,问有多少种表示方法。

我们可以推出dp[j] = dp[j] + dp[ j - coin[i] ],dp[j]代表j块钱的表示方法总数,理解过来就是j块钱可以

通过j-coin[i] 再加上coin[i]来表示。

 

 下面这段代码不太正确(暂时还没有改正):

#include<iostream>
using namespace std;

int dp[7500];
int coin[5] = { 1, 5, 10, 25, 50};

int main()
{
    dp[0] = 1;
    for( int i = 0; i < 5; i ++)
    {
        for( int j = 1; j < 7500; j ++)
        {
            if( j >= coin[i] )
                dp[j] += dp[ j - coin[i] ];
        }
    }
    int cents;
    while( cin >> cents)
    {
        cout << dp[cents] << endl;
    }
    return 0;
}


 网上AC代码(背包):http://www.cnblogs.com/jackge/archive/2013/04/18/3028319.html

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int n,dp[110][300];     // dp[j][k] 统计到第i个coins k钱 dived to j 部分 的分法数
int money[5]={1,5,10,25,50};

int main(){

    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<5;i++)
            for(int j=1;j<=100;j++)
                for(int k=n;k>=money[i];k--)//这样写应该使对的,可写成这样for(int k=money[i];k<=n;k++) 也没WA,估计数据弱了
                    dp[j][k]+=dp[j-1][k-money[i]];
        int ans=0;
        for(int i=0;i<=100;i++) //从0开始,注意 dp[0][0]
            ans+=dp[i][n];
        printf("%d\n",ans);
    }
    return 0;
}
网上背包 http://blog.csdn.net/neofung/article/details/7684462
/******************************************************************************* 
 # Author : Neo Fung 
 # Email : neosfung@gmail.com 
 # Last modified: 2012-06-13 16:07 
 # Filename: acm.cpp 
 # Description :  
 ******************************************************************************/  
#ifdef _MSC_VER  
#define DEBUG  
#define _CRT_SECURE_NO_DEPRECATE  
#endif  
  
#include <fstream>  
#include <stdio.h>  
#include <iostream>  
#include <string.h>  
#include <string>  
#include <limits.h>  
#include <algorithm>  
#include <math.h>  
#include <numeric>  
#include <functional>  
#include <ctype.h>  
using namespace std;  
  
const int kMAX=2500;  
const double kEPS=10E-6;  
  
long long dp[101][kMAX];    //dp[x][y]表示的是在用了x枚硬币的情况下,到达y值的路径数  
  
int main(void)  
{     
  
  int n,ncase=1;  
  
    int value[]={0,1,5,10,25,50};  
    memset(dp,0,sizeof(dp));  
    dp[0][0]=1;  
  
    for (int i=1;i<=5;++i)   //用的硬币  
    {  
        for (int j=1;j<=100;++j) //硬币数目  
        {  
            for (int k=value[i];k<kMAX;++k)  
            {  
                dp[j][k]+=dp[j-1][k-value[i]];  
            }  
        }  
    }  
  while(~scanf("%d",&n))  
  {  
      long long ans=0;  
      for(int j=0;j<=100;++j)  
          ans+=dp[j][n];  
        printf("%lld\n",ans);  
  }  
  
  return 0;  
}  


深搜求解:

他人代码:173ms

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;


__int64 flag;
int num[] = {50, 25, 10, 5, 1};
void dfs(int n, int deep, int last)
{
	if(n<0)	return;
	if(n==0)	
	{flag++;	return;}

	for(int i=deep; i<5; i++)
	{
		if(num[i] <= last)
		{
			last = num[i];
			dfs(n-num[i], deep, last);
		}
	}
	
}

int main()
{
	int n;
	while( scanf("%d", &n)!=EOF)
	{
		flag = 0;
		if(n==0)	{printf("0\n");	continue;}
		if(n >= 50)	dfs(n, 0, num[0]);
		else if(n>=25)	dfs(n, 1, num[1]);
		else if(n>=10)	dfs(n, 2, num[2]);
		else if(n>=5)	dfs(n, 3, num[3]);
		else dfs(n,4, num[4]);
		printf("%I64d\n", flag);
	}
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值