标签:size handle amount class making mis for clu ==
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23514 Accepted Submission(s): 8250
#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> typedef long long ll; using namespace std; const ll mod=1e9; const int maxn=250+50; const int maxm=1; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 int main() { int n; while(scanf("%d",&n)!=EOF) { int ans=0; for(int i=0;i*50<=n;i++) { for(int j=0;j*25<=n;j++) { for(int k=0;k*10<=n;k++) { for(int l=0;l*5<=n;l++) { for(int m=0;m<=n;m++) { if(i*50+j*25+k*10+l*5+m==n&&i+j+k+l+m<=100) ans++; } } } } } cout<<ans<<endl; } return 0; }
思路2:dp思想,dp[i][j]表示使得价值为i,用了 j 个硬币。 可以把硬币的价值用a[]存起来,dp[i][j]=dp[i][j]+dp[i-a[k]][j-1]
具体看代码:
#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> typedef long long ll; using namespace std; const ll mod=1e9; const int maxn=250+50; const int maxm=1; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 int a[5]={1,5,10,25,50}; int dp[maxn][110];//dp[i][j]表示使得价值为i,用了j个硬币的种数 int main() { int n; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp));//初始化都为0 int ans=0; dp[0][0]=1; for(int i=0;i<5;i++)//5种价值的硬币 { for(int j=1;j<=100;j++)//个数 { for(int k=a[i];k<=n;k++)//最小为当前的a[i],一直遍历到n { dp[k][j]+=dp[k-a[i]][j-1];//从上一个加上使用这一个的情况 } } } for(int i=0;i<=100;i++) ans+=dp[n][i]; cout<<ans<<endl; } return 0; }
标签:size handle amount class making mis for clu ==
原文地址:https://www.cnblogs.com/caijiaming/p/9745120.html