//待支付钱币total,钱币种树n,每种钱币不限张数
//使用最少的钱币张数去支付
//样例输入:
//58 4
//样例输出
//7
#include<iostream>
#include<algorithm>
using namespace std;
int total, n, ans;
int a[100];
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
cin >> total >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n,cmp);
for (int i = 0; i < n; i++)
{
/*while (a[i] <= total)
{
total -= a[i];
ans++;
}*/
if (a[i] <= total) //优化
{
int num = total / a[i];
ans += num;
total -= a[i] * num;
}
}
cout << ans;
return 0;
}
贪心算法-钱币支付
于 2023-02-16 18:26:02 首次发布