Blue Bridge Cup-Algorithm Training-Change Money

Problem description
  There are n people lining up in the dining hall to buy Haibei chicken rice. Each serving of Haibei Chicken Rice costs 25 yuan. The strange thing is that everyone has only one banknote in his hand (each banknote has a face value of 25, 50, 100 yuan), and the auntie at the canteen did not have any change at the beginning. Can the aunt in the canteen give everyone change (assuming the aunt in the canteen is smart enough) to
enter the format
  an integer n in the first line, indicating the number of people in line.

Next n integers a[1], a[2],...,a[n]. a[i] represents the value of the banknotes in the hand of the i-th student (the smaller the i, the higher in the team)
  



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
 

Data size and agreement
  n does not exceed 1,000,000
   

Problem-solving idea:
This is how the author understands that all the money in the hands of all the people in the line can be owned by me, and then I will do three things to get them changeReverse Thinking, Assuming that all the money in the queue is in your hands at the beginning, you can make change, and if you find it, it will be YES, otherwise, it will be NO.
1. Make change between 25-50, count the remaining number of
25, make change between 50-100, count the remaining number of 50
, and judge whether the remaining number of 25 is >= the remaining number of 50, if yes, then YES, Otherwise NO.
Let's look at the code first, and then explain it.

#include<stdio.h>
int main()
{
    
    
	int n,i,j,q1=0,q2=0,q3=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
    
    
		scanf("%d",&j);
		if(j==25){
    
    
			q1++;
		}
		if(j==50){
    
    
			q2++;
		}
		if(j==100){
    
    
			q3++;
		}
	}
	q1=q1-q2;//给所有50找零钱25后,剩余25的个数 
	q2=2*q3-q2;//给所有100找零钱50后,剩余50的个数 
	if(q1>=q2){
    
    
		printf("YES");
	}else{
    
    
		printf("NO");
	}
	return 0;
}

Insert picture description here
 


The result is definitely correct, let’s explain the two important lines of code

q1=q1-q2;//给所有50找零钱25后,剩余25的个数 

Why count the remaining number of 25? Suppose we now have 4 people, two 25, one 50, and one 100.

  1. By subtracting the number of 50 from the number of 25, you can determine whether 50 can be used for all the change.
  2. If it can be found, then q1>=0.
  3. On the contrary, q1<0 can not be found. A negative number means how many 25 sheets are still owed to find all 50s.


q2=2*q3-q2;//给所有100找零钱50后,剩余50的个数

Why count the remaining number of 50? It's still the above assumption, which is more intuitive to feel.

  1. At this moment, in the above operation, we already know whether 25 is remaining, but whether there is any remaining, only 25 may be reduced from your hands, and 50 will not be reduced.
  2. We use 50 to make change for all 100s, 100 (2n)-50 (n), 100 is equivalent to 2 50s, to determine how many 50 numbers can be left (that is, q2), q2>= means that all 100s are found to be 50 If you have change, you will have to find another 25 change; otherwise, the change is not over 100.


if(q1>=q2)
  1. Compare the number of 25 remaining on hand with the 50 customers who still need to find 25 change. If q1>=q2, it means that all of them are found or there are more 25 left; otherwise, they cannot be found.
     

Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/114857778