找零钱 解题报告

Code:
  1. 描述:  
  2.   
  3. Poor Bessie has taken a job in the convenience store located just over the border in Slobbovia. Slobbovians use different coinages than the USA; their coin values change day-by-day!  
  4. Help Bessie make optimal change for Slobbovian shoppers. You will need to create C (1 <= C <= 1000) cents of change using N (1 <= N <= 10) coins of various values. All test cases will be solvable using the supplied coins.  
  5. If 5 coins of values 50, 25, 10, 5, and 1 were available, Bessie would make optimum change (minimal coins) of 93 cents by using 1 x 50, 1 x 25, 1 x 10, 1 x 5, and 3 x 1 coins (a total of 7 coins).   
  6. How hard could it be? The final two test cases will be challenging.  
  7.   
  8. 输入:  
  9.   
  10. Line 1: Two space-separate integers: C and N  
  11. Lines 2..N+1: Each line contains a single unique integer that is a coin value that can be used to create change  
  12.   
  13. 输出:  
  14.   
  15. Line 1: A single integer that is the minimum number of coins to create C cents  
  16.   
  17. 输入样例:  
  18.   
  19. 93 5  
  20. 25  
  21. 50  
  22. 10  
  23. 1  
  24. 5  
  25. 输出样例:7  
  26.   
  27.    
  28.   
  29. 解题思路:先想到用贪心法,可以从大的面额开始算,直到超出,然后换成小的面额去求,直到求出一个N,但后来总觉得有个地方可能会做不到,没那么容易贪的,如果面额没有1的时候,是不是可能出现不能刚好的情况, 这个时候就得反过来让大的面额进行减少的处理,有点小麻烦。就想用另一种方法,动态规划,比如既然要求面额为93的最优解,就得求出92、91、90一直到1的最优解,把问题一直小化处理  
  30.   
  31. 例如:面值为50、25、1、10、5       求93的最优解  
  32.   
  33. 要求93得先求43、68、92、83、88的最优解+1,就这样一直求下去  
  34.   
  35. 时间复杂度:O(m*n);  
  36.   
  37. 代码如下:  
  38.   
  39. #include<iostream>  
  40.   
  41. using namespace std ;  
  42.   
  43.    
  44.   
  45. int main()  
  46.   
  47. {  
  48.   
  49.        int n,m;  
  50.   
  51.        int a[500]={0};  
  52.   
  53.        int b[10000]={0};  
  54.   
  55.        cout<<"请输入硬币面值的个数:";  
  56.   
  57.        cin>>n;  
  58.   
  59.        cout<<"请输入各个硬币的面值:";  
  60.   
  61.        for(int i=1;i<=n;i++)  
  62.   
  63.               cin>>a[i];  
  64.   
  65.        cout<<"请输入要找的钱:";  
  66.   
  67.        cin>>m;  
  68.   
  69.        void coin(int n , int m , int a[] , int b[]) ;  
  70.   
  71.        coin(n ,  m , a , b) ;  
  72.   
  73.         cout << "最优解为:" << b[m] << endl ;  
  74.   
  75.         return 0;  
  76.   
  77. }  
  78.   
  79.    
  80.   
  81. void coin(int n , int m , int a[] , int b[])  
  82.   
  83. {  
  84.   
  85.   b[0] = 0 ;  
  86.   
  87.   for(int i=1 ; i<=m ; i++)  
  88.   
  89.   {  
  90.   
  91.         int min = m ;  
  92.   
  93.         for(int j=1 ; j<=n ; j++)                          
  94.   
  95.         {  
  96.   
  97.              if(i>=a[j] && b[i-a[j]] < min)  
  98.   
  99.                  min = b[i-a[j]] ;    
  100.   
  101.          }        
  102.   
  103.         b[i] = min + 1 ;  
  104.   
  105.    }  
  106.   
  107. }  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值