The coin problem code of the programming algorithm (C)

Source: Internet
Author: User

Title: There are 1, 5, 10, 50, 100, 500 yuan coins each several pieces, now to use these coins to pay a dollar, at least how many coins?

There is at least one payment scheme for the fake question.

Use the greedy algorithm, the first choice of the largest coins, and constantly adjust the number of coins.

Code:

* * * main.cpp * *  Created on:2014.7.17 * 
 author:spike/
      
/*eclipse CDT, gcc 4.8.1*/
      
#include <stdio.h>  
#include <limits.h>  
      
#include <utility>  
#include <queue >  
      
using namespace std;  
      
Class Program {  
    const int v[6] = {1, 5, Ten, m};  
    int c[6] = {3, 2, 1, 3, 0, 2};  
    int A = 620;  
      
Public:  
    void Solve () {  
        int ans = 0;  
        for (int i=5; i>=0; i--) {  
            int t = min (A/v[i], c[i]);  
            A-= T*v[i];  
            Ans + = t;  
        }  
        printf ("result =%d\n", ans);  
    }  
;  
      
      
int main (void)  
{program  
    P;  
    P.solve ();  
    return 0;  
}

Output:

result = 6

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Author: csdn Blog Mystra

Related Article

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.