Greedy algorithm--coin problem

Source: Internet
Author: User

There are 1 yuan, 5 yuan, 10 yuan, 50 yuan, 100 yuan, 500 yuan coins each c1,c5,c10,c50,c100,c500 pieces. Now it's time to use these coins to pay a, at least how many coins you need.

#include <iostream>
using namespace std;
int v[6] = {1, 5, 10, 50, 100, 500};//coin face
int c[6] = {3, 2, 1, 3, 0, 2};//coin corresponds to the number of
int count = 0;
int A;
Calculate the minimum required number of coins
void Solve ()
{for
    (int i = 5; 0 <= i; i)
    {
        int t = min (A/v[i], c[i]);
        A-=  v[i] * t;
        Count + = t;
    }
    if (A = = 0)
    cout << count;
    else
    cout << "There is no way!";
}
int main ()
{
    cin >> A;
    Solve ();
    return 0;
}


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.