题目:
有5种硬币,面值分别为:1,5,10,25,50。数量无限,输入非负整数s,选用硬币,使其和为s。要求输出最少的硬币组合要多少个硬币?
数据范围:
s最大为250.
解题思路:
有时间再写,可以评论喊一声哦,我怕我忘记了!!!
代码如下:
#include <iostream>
using namespace std;
const int N = 5;
const int Maxm = 251;int m[] = {1, 5, 10, 25, 50};
const int INF = 99999999;
int dp[Maxm];int main() {for (int i = 0; i < Maxm; i++)dp[i] = INF;dp[0] = 0;for (int i = 0; i < N; i++)for (int j = m[i]; j < Maxm; j++) {dp[j] = min(dp[j], dp[j - m[i]] + 1);}int s;while (cin >> s) {cout << dp[s] << endl;}return 0;
}