From d563f462682dc3d3029d1582fd2715e4b29c0a8d Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Wed, 20 Apr 2016 21:15:34 +0800 Subject: [PATCH] From www.aiuxian.com http://www.aiuxian.com/article/p-2049140.html --- POJ/3040_aiuxian.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 POJ/3040_aiuxian.cpp diff --git a/POJ/3040_aiuxian.cpp b/POJ/3040_aiuxian.cpp new file mode 100644 index 0000000..7277aea --- /dev/null +++ b/POJ/3040_aiuxian.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +using namespace std; +const int maxn=30; +struct node +{ + int x,y;//存储钱的大小和数量 + bool operator<(const node c)const + { + return x>c.x; + } +}num[maxn]; +int need[maxn],n,c,kj; +//need[]函数是是存储需要哪些钱币的组合 +int main() +{ + while(scanf("%d%d",&n,&c)!=EOF) + { + for(int i=0;i0) + { + int t=sum/num[i].x; + if(t==0) + continue; + t=min(t,num[i].y); + need[i]=t; + sum-=t*num[i].x; + } + } + + if(sum>0)//如果在上面的组合中没有使sum变为0,则从小到大选择最小的面值组合,这样使面值超过的最小化 + { + for(int i=n-1;i>=0;i--) + if(num[i].y&&num[i].x>=sum) + { + sum=0; + need[i]++; + break; + } + } + if(sum>0)//如果组合不能达到c,怎么说明搜索结束 + break; + int cc=2e9; + for(int i=n-1;i>=0;i--)//找出这个组合成的面值中最小的数量,同时减去它们。 + { + if(need[i]) + { + cc=min(cc,num[i].y/need[i]); + } + } + kj+=cc; + for(int i=0;i