Problem Description:
There are 1 points, 2 points, 5 points, 10 points four kinds of coins, each coin unlimited number, given the target cents, how many combinations can be combined into the target cent money? (2012 Innovation Workshop)
Solution: (Based on depth-first recursion)
#include <iostream> #include <vector>using namespace std;void dfs (int index, int total, int target, int &co UNT, vector<int> Res, vector<vector<int>> &ans, int a[],int n) {if (total>target) return;if ( Total==target) {//cout<< "count=" <<count<<endl;//for (int i=0;i<res.size (); i++)//{//cout< <res[i]<< "";//}//Cout<<endl;ans.push_back (res); count++;return;} for (int i=index;i<n;i++) {total+=a[i];res.push_back (a[i]);d FS (i,total,target,count,res,ans,a,n); Res.pop_back ( ); total-=a[i];}} int main () {int a[]={1, 2, 5, 10};int n=sizeof (A)/sizeof (int); int index=0;int Total=0;int target=18;int count=0;vector <int> res;vector<vector<int>> Ans;dfs (index, total, target, count, res, ans, A, n);cout<< " Total count= "<<count<<endl;for (int i=0;i<ans.size (); i++) {for (int j=0;j<ans[i].size (); j + +) {cout< <ans[i][j]<< "";} Cout<<endl;} return 0;}
Coin combination Problem