入门级的贪心算法实现。不得不说书上的示例代码真心简介。
题源同样来自《挑战程序竞赛》39页。
//
// 039_coin.cpp
// changlle
//
// Created by user on 12/25/15.
// Copyright (c) 2015 user. All rights reserved.
//
#include <iostream>
using namespace std;
int main(){
int c1=3,c5=2,c10=1,c50=3,c100=0,c500=2,A=620;
int num[6]={c1,c5,c10,c50,c100,c500};
int v[6]={1,5,10,50,100,500};
int i=5;
int ans=0;
while (A>0) {
int tempn=A/v[i];
if (tempn>num[i]) tempn=num[i];
A=A-tempn*v[i];
ans=ans+tempn;
i--;
}
cout<<ans<<endl;
return 0;
}