zoukankan      html  css  js  c++  java
  • Gym

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash register and does not have any change with him.

    The students are going to pay using Jordanian money notes, which are of the following types: 1, 5, 10, 20, 50.

    Given how much each student has to pay, the set of notes he’s going to pay with, and the order in which the students arrive at the cashier, your task is to find out if the cashier will have enough change to return to each of the student when they arrive at the cashier.

    Input

    The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.

    Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.

    The students are given in order; the first student is in front of the cashier.

    It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.

    Output

    Print yes if the cashier will have enough change to return to each of the students when they arrive in the given order, otherwise print no.

    Examples

    Input
    3
    4 0 1 0 0 0
    9 4 1 0 0 0
    8 0 0 1 0 0
    Output
    no
    Input
    3
    9 4 1 0 0 0
    4 0 1 0 0 0
    8 0 0 1 0 0
    Output
    yes
    题意:第一行输入一个n表示,那个人正在排队,当前售货员没有零钱,接下来又n行,每行有6个数,分别表示所需付的钱,以及1,5,10,20,50元的个数,判断售货员是否可以对每个顾客及时找零。
    题解:采用贪心的算法,金额越小越适合找零,能用金额大的找零则用金额大的找零,尽量少用金额小的找零。
    具体情况详见代码:
      1 #include<iostream> 
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<string>
      5 #include<algorithm>
      6 #include<stack>
      7 #include<queue>
      8 #define ll long long
      9 #define inf 0x3f3f3f3f 
     10 using namespace std;
     11 int main(){
     12     int n;
     13     while(cin>>n)
     14     {
     15         int flag=0;
     16         int num1=0,num5=0,num10=0,num20=0,num50=0,a,b,c,d,e,k;
     17         for(int i=0;i<n;i++)
     18         {
     19             cin>>k>>a>>b>>c>>d>>e;
     20             int sum=a+b*5+c*10+d*20+e*50;  //顾客所拥有的总金额 
     21             if(k==sum)   //若刚好够,则全部给售货员 
     22             {
     23                 num1+=a;
     24                 num5+=b;
     25                 num10+=c;
     26                 num20+=d;
     27                 num50+=e;
     28             }
     29             if(k<sum)
     30             {
     31                 sum=sum-k;   //需要找零的钱 
     32                 if(sum>=50&&num50>0)
     33                 {
     34                     if(sum>=num50*50)
     35                     {
     36                         
     37                         sum-=num50*50;  //用50找零后还需要再找的零钱 
     38                         num50=0;
     39                     }
     40                     else
     41                     {
     42                         sum=sum%50;
     43                         num50-=sum/50;
     44                     }
     45                 }
     46                 if(sum>=20&&num20>0)
     47                 {
     48                     if(sum>=num20*20)
     49                     {
     50                         
     51                         sum-=num20*20;
     52                         num20=0;
     53                     }
     54                     else
     55                     {
     56                         sum=sum%20;
     57                         num20-=sum/20;
     58                     }
     59                 }
     60                 if(sum>=10&&num10>0)
     61                 {
     62                     if(sum>=num10*10)
     63                     {
     64                         
     65                         sum-=num10*10;
     66                         num10=0;
     67                     }
     68                     else
     69                     {
     70                         sum=sum%10;
     71                         num10-=sum/10;
     72                     }
     73                 }
     74                 if(sum>=5&&num5>0)
     75                 {
     76                     if(sum>=num5*5)
     77                     {
     78                         
     79                         sum-=num5*5;
     80                         num5=0;
     81                     }
     82                     else
     83                     {
     84                         sum=sum%5;
     85                         num5-=sum/5;
     86                     }
     87                 }
     88                 if(sum>num1)
     89                     flag=1;
     90                 else
     91                     num1-=sum;
     92                 num1+=a;
     93                 num5+=b;
     94                 num10+=c;
     95                 num20+=d;
     96                 num50+=e;
     97             }
     98         }
     99         if(flag==1) cout<<"no"<<endl;
    100         else   cout<<"yes"<<endl;
    101     }
    102     return 0;
    103 }
  • 相关阅读:
    Java:synchronized关键字引出的多种锁
    Java:Web Service初入门
    Java:HashMap原理与设计缘由
    Java:集合类的数据结构
    NoSQL数据库兴起
    Hadoop介绍与安装
    Java:泛型的理解
    《代码整洁之道》总结和笔记
    shell运算
    shell变量
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9274130.html
Copyright © 2011-2022 走看看