`
wusuoya
  • 浏览: 629464 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论

动态规划找零问题O(nk)

阅读更多

主要思路是,用一个数组coinsUsed[]来保存找i分钱所需的硬币数,(i==maxchange就是我们正在寻找的解),用一个数组lastCoin[]来保存哪一个硬币是最后用来得到最佳找零方案的信息。coins[] 用来记录硬币零钱有哪几种,differentCoins记录下coins[]的长度。maxChange记录最后的要兑换的零钱数。该算法复杂度为O(NK),N为不同面值的硬币数目,K是我们要找的的零钱数量。

 

 

 

Java代码   收藏代码
  1. public final class MakeChange  
  2. {  
  3.     // Dynamic programming algorithm to solve change making problem.  
  4.     // As a result, the coinsUsed array is filled with the  
  5.     // minimum number of coins needed for change from 0 -> maxChange  
  6.     // and lastCoin contains one of the coins needed to make the change.  
  7.     public static void makeChange( int [ ] coins, int differentCoins,  
  8.                 int maxChange, int [ ] coinsUsed, int [ ] lastCoin )  
  9.     {  
  10.         coinsUsed[ 0 ] = 0; lastCoin[ 0 ] = 1;  
  11.   
  12.         forint cents = 1; cents <= maxChange; cents++ )  
  13.         {  
  14.             int minCoins = cents;  
  15.             int newCoin  = 1;  
  16.   
  17.             forint j = 0; j < differentCoins; j++ )  
  18.             {  
  19.                 if( coins[ j ] > cents )   // Cannot use coin j  
  20.                     continue;  
  21.                 if( coinsUsed[ cents - coins[ j ] ] + 1 < minCoins )  
  22.                 {  
  23.                     minCoins = coinsUsed[ cents - coins[ j ] ] + 1;  
  24.                     newCoin  = coins[ j ];  
  25.                 }  
  26.             }  
  27.   
  28.             coinsUsed[ cents ] = minCoins;  
  29.             lastCoin[ cents ]  = newCoin;  
  30.         }  
  31.     }  
  32.   
  33.     // Simple test program  
  34.     public static void main( String [ ] args )  
  35.     {  
  36.         // The coins and the total amount of change  
  37.         int numCoins = 5;  
  38.         int [ ] coins = { 15102125 };  
  39.         int change = 0;  
  40.   
  41.         if( args.length == 0 )  
  42.         {  
  43.             System.out.println( "Supply a monetary amount on the command line" );  
  44.             System.exit( 0 );  
  45.         }  
  46.   
  47.         try  
  48.           { change = Integer.parseInt( args[ 0 ] ); }  
  49.         catch( NumberFormatException e )  
  50.         {  
  51.             System.out.println( e );  
  52.             System.exit( 0 );   
  53.         }  
  54.   
  55.         int [ ] used = new int[ change + 1 ];  
  56.         int [ ] last = new int[ change + 1 ];  
  57.   
  58.         makeChange( coins, numCoins, change, used, last );  
  59.   
  60.         System.out.println( "Best is " + used[ change ] + " coins" );  
  61.   
  62.         forint i = change; i > 0; )  
  63.         {  
  64.             System.out.print( last[ i ] + " " );  
  65.             i -= last[ i ];  
  66.         }  
  67.         System.out.println( );  
  68.     }  

详细解释可参见教材P89

 

分享到:
评论
1 楼 msn877763580 2011-10-06  
数据结构与问题求解------

相关推荐

Global site tag (gtag.js) - Google Analytics