标签:style blog class code c java
问题描述:超市有4种包装的鸡蛋,分别是3个一盒,6个一盒,9个一盒和20个一盒。问顾客要买N个鸡蛋时,所有的组合方案。(Morgen Stanley 2014 Intern).
1 void BuyeggsCore(vector<const int> &coins, const int target, int sum, int i, vector<int> &count, vector<vector<int>> &ret) { 2 if (i >= 4) return; 3 int left_value = target - sum; 4 for (int j = 0; j * coins[i] <= left_value; ++j) { 5 sum = target - left_value + j * coins[i]; 6 count[i] = j; 7 if (sum == target) { 8 ret.push_back(count); 9 } 10 else if (sum < target) { 11 BuyeggsCore(coins, target, sum, i + 1, count, ret); 12 } 13 } 14 } 15 16 vector<vector<int>> BuyEggs(vector<const int> &coins, const int target) { 17 vector<vector<int>> ret; 18 vector<int> count; 19 count.resize(coins.size()); 20 BuyeggsCore(coins, target, 0, 0, count, ret); 21 return ret; 22 } 23
标签:style blog class code c java
原文地址:http://www.cnblogs.com/dengeven/p/3725337.html