您的位置:首页 > 其它

poj 2818 Making Change 枚举

2015-12-11 17:06 357 查看
//poj 2818
//sep9
include <iostream>
using namespace std;
int Q,D,N,P,C;
struct NODE
{
int a,b,c,d;
};
pair<int,NODE> mymin(pair<int,NODE> x,pair<int,NODE> y)
{
if(x.first<y.first) return x;
return y;
}
void solve()
{
NODE x;
pair<int,NODE> ans=make_pair(INT_MAX,x);
for(int a=Q;a>=0;--a)
for(int b=D;b>=0;--b)
for(int c=N;c>=0;--c)
for(int d=P;d>=0;--d){
if(C==a*25+b*10+c*5+d){
int sum=a+b+c+d;
NODE tmp;
tmp.a=a,tmp.b=b,tmp.c=c,tmp.d=d;
ans=mymin(ans,make_pair(sum,tmp));
}

}
if(ans.first==INT_MAX)
puts("Cannot dispense the desired amount.");
else
printf("Dispense %d quarters, %d dimes, %d nickels, and %d pennies.\n",ans.second.a,ans.second.b,ans.second.c,ans.second.d);
}

int main()
{
while(scanf("%d%d%d%d%d",&Q,&D,&N,&P,&C)==5){
if(Q+D+N+P+C==0) break;
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 算法