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