Dynamic Planning-Change

Source: Internet
Author: User

Title Description

Want to exchange 100 yuan, have 1,2,5,10 four kinds of money, ask how many Exchange method in total

Recursive solution
#include <iostream>using namespace STD;Const intN = -;intDimes[] = {1,2,5,Ten};intarr[n+1] = {1};intCoinexchangerecursion (intNintM//recursive approach for better understanding{if(n = =0)//Jump out of recursion condition    return 1;if(N <0|| m = =0)return 0;return(Coinexchangerecursion (n, M-1) + coinexchangerecursion (n-dimes[m-1], M));//Divided into two cases: in exchange for the current face value of the situation + not in exchange for the current face value}intMain () {intNum=coinexchangerecursion (N,4);cout<<num<<endl;return 0; }
Non-recursive solution
#include <iostream>using namespace STD;Const intN = -;intDimes[] = {1,2,5,Ten};intarr[n+1] = {1};intCoinexchange (intN//non-recursive implementation{intI, J;//i from 0 to 3 because each arr[j] is supposed to be redeemed dimes[i], so we're going to go through   for(i =0; I <sizeof(dimes)/sizeof(int); i++) { for(j = dimes[i]; j <= N; j + +)Arr[j], arr[j] = Arr[j] + arr[j-dimes[i]],      //corresponds to the recursive method above: Arr[j] is coinexchangerecursion (n, m-1),      //arr[j-dimes[i]] is coinexchangerecursion (N-dimes[m-1], m)ARR[J] + = arr[j-dimes[i]]; }returnArr[n]; }intMain () {intNum2=coinexchange (N);cout<<num2<<endl;return 0;}
Method Summary

The classic of dynamic programming breaks down big problems into small problems
Recursive algorithm: 100 yuan for the change: there is this face value in small change and the two cases without this denomination, notice the condition of the end of recursion
Non-recursive algorithm: Change 100 of change, then first change 1, 2 、...... Of the change, this algorithm, preferably converted into steps to understand the problem of the way
Careful reasoning can be seen arr[j] = arr[j-dimes[0]] [arr[j-dimes[1]] + arr[j-dimes[2]] + arr[j-dimes[3] [j-dimes[i]>=0]

#include <iostream>#include <vector> //std::vector #include <algorithm> //std::count using namespace STD;Const intN = -;intDimes[] = {1,2,5,Ten};intarr[n+1] = {1}; vector<int>vvintCoinexchangerecursion (intNintM//recursive approach for better understanding{if(n = =0) {intI for(i =0; I <sizeof(dimes)/sizeof(int); i++) {intCNT = count (Vv.begin (), Vv.end (), dimes[i]);cout<< Dimes[i] <<": "<< CNT <<"\ T"; }cout<< Endl;return 1; }//Jump out of recursion condition    if(N <0|| m = =0)return 0; Vv.push_back (dimes[m-1]);intYes = Coinexchangerecursion (n-dimes[m-1], M); Vv.pop_back ();intNo = Coinexchangerecursion (n, M1);return(No+yes);//is divided into two cases, if not the current coin, then how much? Plus, if you change the current coin, the total value is reduced, how many methods of redemption at this time? }intCoinexchange (intN//non-recursive implementation{intI, J; for(i =0; I <sizeof(dimes)/sizeof(int); i++)//i from 0 to 3 because each arr[j] is supposed to be redeemed dimes[i], so we're going to go through{ for(j = dimes[i]; j <= N; j + +)Arr[j], arr[j] = Arr[j] + arr[j-dimes[i]],            //corresponds to the recursive method above: Arr[j] is coinexchangerecursion (n, m-1),            //arr[j-dimes[i]] is coinexchangerecursion (N-dimes[m-1], m)ARR[J] + = arr[j-dimes[i]]; }returnArr[n]; }intMainintargcChar*argv[]) {intNum=coinexchangerecursion (N,4);cout<<num<<endl;intNum2=coinexchange (N);cout<<num2<<endl;return 0; }

Reference:

    • Http://www.tuicool.com/articles/VBreAnY
    • Http://taop.marchtea.com/02.05.html

Dynamic Planning-Change

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.