码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 找零钱方法数

时间:2018-08-01 11:50:07      阅读:920      评论:0      收藏:0      [点我收藏+]

标签:clu   str   getc   getch   turn   out   ++   get   stat   

* 动态规划法

#include "stdafx.h"
#include <iostream>
#include <vector>

using std::vector;

class Solution {
public:
	static int coins(vector<int> deno, int v) {
		if (deno.size() == 0 || v < 0) {
			return 0;
		}
		vector<int> dp = vector<int>(v + 1, 0);
		for (int j = 0; deno[0] * j <= v; j++) {
			dp[deno[0] * j] = 1;
		}
		for (int i = 1; i < deno.size(); i++) {
			for (int j = 1; j <= v; j++) {
				dp[j] += j - deno[i] >= 0 ? dp[j - deno[i]] : 0;
			}
		}
		return dp[v];
	}
};

int main()
{
	vector<int> deno = { 50, 20, 10, 5, 1 };
	Solution sln;
	int solutions = sln.coins(deno, 100);
	std::cout << solutions << "\n";
	getchar();

    return 0;
}

  343

* 数学问题

   maths is fun

   https://www.mathsisfun.com/algebra/index.html

C++ 找零钱方法数

标签:clu   str   getc   getch   turn   out   ++   get   stat   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9399450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!