An algorithm problem-money exchange

Source: Internet
Author: User

  Title: All values in the given array Arr,arr are positive and not duplicated. Each value represents a denomination of the currency, each denomination of the currency can use any one, and then given an aim to represent the amount of money to find how many ways to change money. The code is as follows:

  

1#include <iostream>2#include <vector>3 using namespacestd;4 //Dynamic Planning5 classExchange {6  Public:7     intCountways (vector<int> Penny,intNintaim)8     {9         if(n==0|| aim<0)Ten             return 0; One         int**map=New int*[n]; A          for(intI=0; i<n;i++) -map[i]=New int[aim+1]; -          for(intI=0; i<n;i++) the              for(intj=0; j<aim+1; j + +) -map[i][j]=-1; -          for(intI=0; i<n;i++) -map[i][0]=1; +          for(intI=0; i<aim+1; i++) -             if(i%penny[0]==0) +map[0][i]=1; A             Else  atmap[0][i]=0; -          for(intI=1; i<n;i++) -              for(intj=1; j<aim+1; j + +) -                 if((J-penny[i]) >=0) -map[i][j]=map[i][j-penny[i]]+map[i-1][j]; -                 Else inmap[i][j]=map[i-1][j]; -         returnmap[n-1][aim]; to     } +  - }; the  *  $ /*Memory SearchPanax Notoginseng class Exchange { - Public : the int countways (vector<int> penny, int n, int aim) +     { A if (n==0| | aim<0) the return 0; + int **map=new int*[n]; - for (int i=0;i<n;i++) $ map[i]=new int[aim+1]; $ for (int i=0;i<n;i++) - for (int j=0;j<aim+1;j++) - Map[i][j]=-1; the return process (PENNY,0,AIM,MAP); - Wuyi     } the int process (vector<int> arr,int index,int aim,int **map) -     { Wu int res; - if (arr.size () -1==index) About         { $ if (map[index][aim]==-1) - map[index][aim]= (Aim%arr[index])? 0:1; - return Map[index][aim]; -         } A Else +         { the res=0; - int K=aim/arr[index]; $ for (int i=0;i<=k;i++) the             { the if (map[index+1][aim-i*arr[index]]==-1) the map[index+1][aim-i*arr[index]]=process (arr,index+1,aim-i*arr[index],map); the Res+=map[index+1][aim-i*arr[index]]; -             } in         } the return res; the     } About };*/ the  the /*Violent Search the class Exchange { + Public : - int countways (vector<int> penny, int n, int aim) the     {Bayi if (n==0| | aim<0) the return 0; the return process (Penny,0,aim); -  -     } the int process (vector<int> arr,int index,int aim) the     { the int res; the if (arr.size () -1==index) -         { the res= (Aim%arr[index])? 0:1; the return res; the         }94 Else the         { the res=0; the int K=aim/arr[index];98 for (int i=0;i<=k;i++) About res+=process (Arr,index+1,aim-i*arr[index]); -         }101 return res;102     }103 };*/104  the intMain ()106 {107     inta[3]={1,2,3};108vector<int> Arr (a,a+3);109 Exchange E; theCout<<e.countways (arr,3,3);111      the     return 0;113}

  

An algorithm problem-money exchange

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.