动态规划 硬币兑换

参考
// 最少硬币.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 
using namespace std;
class hh 
{
public:
	vector ans;
	vector coins = {1,2,5 };
	void fun(int i,int num) 
	{
		if (i == 0) 
		{
			ans.push_back(0);
			fun(i + 1, num);
		}
		else 
		{
			int min = 999;
			for (auto x : coins) 
			{
				if (i >= x&&ans[i - x] + 1 < min) 
				{
					min = ans[i - x] + 1;
				}
			}
			ans.push_back(min);
			if (i < num) 
			{
				fun(i + 1, num);
			}
		}
	}
};
int N=9;//总金额
int main()
{

	hh h;
	h.fun(0, N);
	for (int i = 0; i <= N; i++) 
	{
		cout << h.ans[i]<< endl;
	}
	system("pause");
    return 0;
}


https://www.cnblogs.com/snowInPluto/p/5992846.html

你可能感兴趣的:(算法,动态规划)