# include # include # include using namespace std; # include "change_dynamic.hpp" //****************************************************************************80 int *change_dynamic ( int coin_num, int coin_value[], int target ) //****************************************************************************80 // // Purpose: // // change_dynamic() solves the change making problem with dynamic programming. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 June 2017 // // Author: // // John Burkardt // // Input: // // int COIN_NUM, the number of coin denomiations. // // int COIN_VALUE[COIN_NUM], the value of each coin. // These values should be positive integers. // // int TARGET, the desired sum. // // Output: // // int change_dynamic_LIST[TARGET+1], A(T) lists // the smallest number of coins needed to form the sum T, or "Inf" if // it is not possible to form this sum. // { int *a; int i; int i4_huge = 2147483647; int j; a = new int[target+1]; a[0] = 0; for ( j = 1; j <= target; j++ ) { a[j] = i4_huge; } // // If T is the value of a coin, then A(T) is 1. // for ( i = 0; i < coin_num; i++ ) { if ( coin_value[i] <= target ) { a[coin_value[i]] = 1; } } // // To compute A(T) in general, consider getting there by adding // one coin of value V, and looking at A(T-V). // for ( j = 1; j <= target; j++ ) { for ( i = 0; i < coin_num; i++ ) { if ( 0 <= j - coin_value[i] ) { a[j] = min ( a[j] - 1, a[j-coin_value[i]] ) + 1; } } } return a; }