Training algorithm looking for change

Resource constraints
Time limit: 1.0s Memory Limit: 256.0MB
Problem Description
  There are n individuals are queuing up to buy canteen rambling chicken rice. Each rambling chicken rice for 25 yuan. The strange thing is, everyone hands of only one bill (face value of each note 25, 50 dollars), and a canteen aunt began without any change. Will the canteen aunt could give everyone the change (assuming that the canteen aunt smart enough)
Input Format
  The first line an integer n, the number of people queuing.

  Then n integers a [1], a [2 ], ..., a [n]. a [i] represents the value of the hands of the i-th students banknotes (i smaller, more toward the front in the ranks)
Output Format
  Output YES or NO
Sample input
4
25 25 50 50
Sample Output
YES
Sample input
2
25 100
Sample Output
NO
Sample input
4
25 25 50 100
Sample Output
YES
Scale data and conventions
  n is not more than 1 million
 
We need to consider a variety of circumstances, namely 25, 50 and 100 yuan three cases.
 1 #include<iostream>
 2 //author:QIANG
 3 using namespace std;
 4 int main(){
 5     int n,s;
 6     cin>>n;
 7     int tf=0,f=0;
 8     int flag=1;
 9     for(int i=0;i<n;i++){
10         cin>>s;
11         if(flag==1){
12             if(s==25){
13                 tf++;
14             }else if(s==50){
15                 if(tf>0){
16                     tf--;
17                     f++;
18                 }else{
19                     flag=0;
20                 }
21             }else{
22                 if(f>0){
23                     f--;
24                     s=s-50;
25                 }
26                 if(s==50){
27                     if(tf>0){
28                         tf--;
29                     }else{
30                         flag=0;
31                     }
32                 }else{
33                     if(tf>=3){
34                         tf=tf-3;
35                     }else{
36                         flag=0;
37                     }
38                 }
39             }
40         }
41     }
42     if(flag==1){
43         cout<<"YES";
44     }else cout<<"NO";
45     return 0; 
46 } 

 

Guess you like

Origin www.cnblogs.com/zq-dmhy/p/12312554.html
Recommended