hdu2069Coin Change(暴力求解----动态规划(背包)求解---搜索--)
2013-11-02 00:58
381 查看
Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11533 Accepted Submission(s): 3845
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
[align=left]Output[/align]
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
[align=left]Sample Input[/align]
11
26
[align=left]Sample Output[/align]
4
13
[align=left]Author[/align]
Lily
[align=left]Source[/align]
浙江工业大学网络选拔赛
//用C写0ms,java 170ms,注意for循环的结束判定的剪枝,( 很暴力)
用动态规划求解:
我们手中有50、25、10、5、1五种面额的硬币。然后要用这些硬币来表示j块钱,问有多少种表示方法。
我们可以推出dp[j] = dp[j] + dp[ j - coin[i] ],dp[j]代表j块钱的表示方法总数,理解过来就是j块钱可以
通过j-coin[i] 再加上coin[i]来表示。
下面这段代码不太正确(暂时还没有改正):
网上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]
;
printf("%d\n",ans);
}
return 0;
}网上背包http://blog.csdn.net/neofung/article/details/7684462:
深搜求解:
他人代码:173ms
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11533 Accepted Submission(s): 3845
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
[align=left]Output[/align]
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
[align=left]Sample Input[/align]
11
26
[align=left]Sample Output[/align]
4
13
[align=left]Author[/align]
Lily
[align=left]Source[/align]
浙江工业大学网络选拔赛
//用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 ++; } 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]
;
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] ; 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背包问题
- 二叉树搜索求解背包问题
- 【暴力搜索】【动态规划】[NOIP 1999]邮票面值设计
- 0-1背包问题,动态规划求解
- 暴力搜索解0-1背包问题
- 算法实践——改良的求解数独的暴力搜索法
- 动态规划求解0-1背包问题
- 背包问题,动态规划求解,matlab代码,c++代码
- 动态规划求解0-1背包
- 算法导论第16章 贪心算法-0-1背包问题—动态规划求解
- 动态规划求解0-1背包问题
- 动态规划求解0-1背包
- Java 动态规划求解0-1背包问题
- 动态规划求解背包问题(java版本)
- 0-1背包问题及其动态规划求解之二——王晓东的书本解法
- 动态规划求解背包问题(JAVA实现)
- 记忆搜索与动态规划——DP背包问题
- 01背包 暴力搜索
- 用动态规划求解0-1背包问题
- 动态规划求解0-1背包问题