Backpack or BFS
The meaning of this question is to say that you have several basic currencies ~ The minimum amount of money used in the base currency is 100.
The probability of output usage and the maximum usage.
BFS or backpack.
But remember to open the array bigger. 100 = 99 + 99-98 may occur.
A backpack is used to create a complete backpack and obtain the minimum possible sum.
Then we made a 01 backpack to see if it could be reduced.
Backpack:
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;int dp[10002];int value[7];int main(){ int t; scanf("%d",&t); while(t--) { int m=10001; for(int i=0; i<6; i++) scanf("%d",&value[i]); for(int i=1; i<m; i++) dp[i]=10001; dp[0]=0; for(int i=0; i<6; i++) { for(int j=value[i]; j+value[i]<m; j++) { dp[j]=min(dp[j-value[i]]+1,dp[j]);// printf("%d :%d==\n",j,dp[j]); } } for(int i=0; i<6; i++) { for(int j=m-value[i]; j>0; j--) { dp[j]=min(dp[j+value[i]]+1,dp[j]);// printf("%d :%d==\n",j,dp[j]); } } double ans=0; int maxn=0; for(int i=1; i<=100; i++) {// printf("%d : %d\n",i,dp[i]); ans+=dp[i]; maxn=max(maxn,dp[i]); } printf("%.2f %d\n",ans/100.0,maxn); }}
BFS:
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;struct lx{ int ans,lv;};int ans[2051];int value[7];void bfs(){ queue<lx>q; bool vis[2051]; memset(vis,0,sizeof(vis)); lx now,tmp; tmp.ans=0,tmp.lv=0; q.push(tmp); vis[0]=1; while(!q.empty()) { tmp=q.front();q.pop(); ans[tmp.ans]=tmp.lv; for(int i=0;i<6;i++) { int num1=tmp.ans+value[i]; int num2=tmp.ans-value[i]; now.lv=tmp.lv+1; if(!vis[num1]&&num1>0&&num1<2000) { vis[num1]=1; now.ans=num1; q.push(now); } if(!vis[num2]&&num2>0&&num2<2000) { vis[num2]=1; now.ans=num2; q.push(now); } } }}int main(){ int t; scanf("%d",&t); while(t--) { for(int i=0; i<6; i++) scanf("%d",&value[i]); bfs(); double an=0; int maxn=0; for(int i=1;i<=100;i++) { an+=ans[i]; maxn=max(maxn,ans[i]);// printf("%d : %d\n",i,ans[i]); } printf("%.2f %d\n",an/100,maxn); }}