jw910731

零錢問題

Oct 29th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. /**********************************************************************************/
  2. /*  Problem: b028 "忙碌的超商店員" from 動態規劃-最少零錢數                                       */
  3. /*  Language: C++                                                                 */
  4. /*  Result: AC (3ms, 132KB) on ZeroJudge                                          */
  5. /*  Author: jw910731 at 2018-07-25 14:00:13                                       */
  6. /**********************************************************************************/
  7.  
  8. #include <cstdio>
  9. using namespace std;
  10. int dp[101] = {0},coin[6]={1,5,10,12,16,20};
  11. void dp_gen(){
  12.     for(int i=0;i<6;i++) dp[coin[i]] = 1;
  13.     for(int i=1;i<=100;i++){
  14.         int min = dp[i-1];
  15.         for(int j=0;j<6;j++){
  16.             if(i>=coin[j]){
  17.                 if(dp[i-coin[j]] < min){
  18.                     min = dp[i-coin[j]];
  19.                 }
  20.             }
  21.         }
  22.         dp[i] = min+1;
  23.     }
  24. }
  25. int main(){
  26.     dp_gen();
  27.     int n;
  28.     scanf("%d",&n);
  29.     printf("%d\n",dp[n]);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment