2018招商银行笔试编程题:找零钱方案

输入:第一行输入t表示测试用例数,之后每两行是一个测试用例,前一行输入零钱种类数和目标金额,后一行列出零钱面值;

输出:每行输出一个方案数;

代码如下:

#include
#include
#include
#include
using namespace std;


long long coinChange(vector table, int target) {
	sort(table.begin(), table.end());
	vector> dp(table.size(), vector(target + 1, 0));
	for (int i(0); i <= target; ++i) {
		if (i % table[0] == 0)
			dp[0][i] = 1;
	}
	for (int i(1); i < table.size(); ++i) {
		for (int j(0); j <= target; ++j) {
			int k = 0;
			while (j - k * table[i] >= 0) {
				dp[i][j] += dp[i - 1][j - k * table[i]];
				++k;
			}
			
		}
	}

	return dp[table.size() - 1][target];
}

int main() {
	int t, n, k;
	cin >> t;
	vector target;
	vector> cash_table;
	for (int i(0); i < t; ++i) {
		cin >> n >> k;
		target.push_back(k);
		vector temp(n, 0);
		for (int j(0); j < n; ++j)
			cin >> temp[j];
		cash_table.push_back(temp);
	}
	for (int i(0); i < t; ++i)
		cout << coinChange(cash_table[i], target[i]) % 100000007 << endl;
}

你可能感兴趣的:(2018招商银行笔试编程题:找零钱方案)