Greedy algorithm (payroll)

Source: Internet
Author: User

Salary:)
problem:430 Time limit:1000ms Memory limit:65536k
Description
As a teacher of Hangzhou electric, the most expected day is the monthly number 8th, because this day is a pay day, the breadwinner depends on it, hehe but for the school Finance Department staff, this day is a very busy day, finance department of Xiao Hu teacher recently was thinking about a problem: if every teacher's salary is known, At least how many yuan to prepare, in order to give each teacher pay the time without the teacher change it? This assumes that the teacher's wages are positive integers, unit yuan, a total of 100 yuan, 50 yuan, 10 yuan, 5 yuan, 2 yuan and 1 Yuan six.
Input
The input data contains multiple test instances, and the first line of each test instance is an integer n (n<100), which indicates the number of teachers and then the wages of N teachers. N=0 indicates the end of the input and does not handle it.
Output
Output an integer x for each test instance to indicate the minimum number of renminbi to be prepared. Each output occupies one row.
Sample_input
31 2 30
Sample_output
4

1#include <stdio.h>2 intMain ()3{intn,i,j,num,money,s,temp; 4      inty[6]={ -, -,Ten,5,2,1}; 5       while(SCANF ("%d", &n)!=eof && n!=0)    6     {        7num=0; 8          for(i=0; i<n;i++)        9         {            Tenscanf"%d",&Money );  One              for(j=0;j<6; j + +)             A             {                 -s=money/Y[j]; -temp=money%Y[j]; thenum+=s;  -                 if(temp==0) Break;  -money=temp;  -             }         +         }         -printf"%d\n", num);  +        } A}

What is a greedy algorithm? The so-called greedy algorithm refers to, in the problem solving, always make the best choice in the present. In other words, not considering the overall optimal solution, it makes only the local optimal solution in a certain sense .

The greedy algorithm can not get the whole optimal solution for all the problems, but many problems of wide range can produce the approximate solution of the whole optimal solution or the whole optimal solution.

The basic idea of greedy algorithm is as follows:

1. Build a mathematical model to describe the problem.

2. Divide the problem of solving into several sub-problems.

3. To solve each sub-problem, the local optimal solution of each sub-problem is obtained.

4. The local optimal solution of each sub-problem is synthesized as a solution to the original problem.

The process of implementing the algorithm:

Starting from a certain initial state of the problem;

While you can move forward one step toward a given total target

Finding a solution element of a feasible solution;

A feasible solution to the problem of the combination of all solution elements;

Greedy algorithm (payroll)

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.