Coin combination Problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.