About violent enumeration, you should know these

Here is an explanation column about the types of Lanqiao Cup algorithms. Some algorithms such as enumeration algorithms, recursive algorithms, dynamic programming algorithms, etc., are explained in a graphical way. Welcome students to watch, this article will be published on the official account : vitality algorithm .
Welcome everyone to pay attention to the official account to get the latest articles and related preparation materials.

Preface

Speaking of the Blue Bridge Cup, you may be talking about the Violence Cup. Through some past questions, you can find that the first few questions in the past years have been enumerated to find the best answer. Even in the last few questions, you can use violence enumeration to run. Give some answers and get some points. It can be said that those who get violence have the world, let's talk about violent enumeration below. The figure below is an outline of this article.
Insert picture description here

Enumeration definition

When you first came into contact with algorithms, the basic algorithms that everyone practiced were all about enumeration algorithms, such as printing out a 9 * 9 multiplication table, finding the number of daffodils, finding prime numbers, etc., and topics like this are always in a certain Find the answer that meets the conditions within the range of
The core idea of ​​the enumeration algorithm is: enumerate all possible

Case introduction

Title description

Output all the "daffodil numbers". The so-called "daffodil number" refers to a three-digit number whose cube sum is equal to the number itself. For example, 153 is the "daffodil number" because: 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3.

Topic analysis

The first step: what we have to determine is what the loop variable we want to determine here is, it is easy to think that the variable is the three-digit number, this step determines our variable.

Step 2: Determine the range of our variable. Only after the range is determined, we can know which numbers to find. We can know that this number is an integer, and then it is a three-digit number.

Step 3: After we already know where to find In the case of, it is traversed one by one and judged. Here we are going to split the three digits, three times in turn, then add them, and finally make a judgment.

Topic code

#include<iostream>
using namespace std;
//判断条件
bool check(int num)
{
    
    
	int a = num%10;
	int b = num/10%10;
	int c = num/100;
	return a*a*a + b*b*b + c*c*c == num;
} 
int main()
{
    
    
    //确定范围
	for(int i = 100; i <= 999; i++)
	{
    
    
		if(check(i))
		{
    
    
			cout << i <<ends;
		}
	}
	return 0;
} 

Enumeration template

According to our analysis above, there are a total of 3 steps, here I summarize it as 3W:

  • Who: Represents several loop variables, there are several loop variables, there are several loops
  • Where: Represents the variable range.
  • How: Indicates how to operate and filter.
    The operation and judgment of loop variables is the focus of this template
//简单版本

//how:筛选满足的条件
bool check(*arg)
{
    
    
    
}
int main(){
    
    
    /*who:确定几个变量就有几个循环*/
    /*where:判断变量的范围,模板中的 a,b表示范围*/
    for(int i = a; i < b; i++)
    {
    
    
        for(int j = a; j < b; j++)
        {
    
    
            ...
            for(int z = a; z < b; z++)
            {
    
    
                //how1.对变量进行操作。
                
                //how2.对变量进行判断
                if(check(*args))
                {
    
    
                    //do
                }
            }
        }
    }
}

Vitality training

Topic 1: Beer and beverages

Beer is 2.3 yuan per can and beverage is 1.9 yuan per can. Xiao Ming bought some beer and drinks for a total of 82.3 yuan. We also know that he bought less beer than drinks. Please count how many cans he bought.

Problem analysis:

  • who: The loop variables in this question are the number of beer and the number of drinks
  • where: The minimum number of beer is 1, and the maximum is 82.3/2.3. The minimum number of beverages is 1, and the maximum number is 82.3/1.9
  • how: There are two conditions for judging: 1. The quantity of beer purchased is less than the quantity of drinks, 2. The total cost is 82.3.
  • For how to judge the equality of two floating-point types, use the fabs function

Question answer

#include<iostream>
#include<math.h>
using namespace std;
bool check(int i,int j)
{
    
    
	//判断价格是否等于82.3 
	return (fabs(i*2.3+j*1.9 - 82.3) <= 1e-5); 
} 
int main()
{
    
    
	//啤酒 
	for(int i = 0; i < 82.3/2.3; i++)
	{
    
    
		//饮料 
		for(int j = i+1;j < 82.3/1.9; j++)
		{
    
    
			if(check(i,j))
			{
    
    
				cout << i <<ends << j <<endl;
			}
		}
	}
	return 0;
}

Topic 2: Running training

Xiao Ming is going to do a running training.
At the beginning, Xiao Ming is full of physical strength, and his physical strength is calculated as 10,000. If Xiao Ming runs, he loses 600 stamina per minute. If Xiao Ming rests, he will increase his physical strength by 300 per minute. The loss and increase in physical strength change uniformly.

Xiao Ming intends to run for one minute, rest for one minute, run for another minute, and rest for another minute... and so on. If Xiao Ming's physical strength reaches 0 at some point, he will stop exercising.

How long will Xiao Ming stop exercising? To make the answer an integer, please output the answer in seconds. Only fill in the number and not the unit in the answer.

Topic analysis

  • who: In this question, physical strength is a loop variable, so there is only one loop
  • where: For the change of physical strength in the question, the initial value is 1000, and the cycle ends when the physical strength is zero. Here we are not clear
  • how: To judge when the physical strength value is zero, we can output the time and jump out of the loop at the same time

Question answer

#include<iostream>

using namespace std;

int main()
{
    
    
	int n = 10000;//体力最开始的值
	int run = 600/60;//每秒的消耗 
	int time = 0;//时间
	//who:循环变量
	while(n)
	{
    
    
	    //how2:判断
	    //当剩余体力不满足再跑一次时候,压榨最后一点体力,然后跳出循环
	    if(n-600 < 0)
	    {
    
    
	        time += n/run;
	        break; 
	    }
	    
	    //how1:对体力进行操作
	    //跑步
	    n -= 600;
	    n += 300;
	    //加上两分钟
	    time += 120;
	}
	cout << time << endl;
	return 0;
} 

Question style

The basic application of violent enumeration is the above, and the style of the Blue Bridge Cup is constantly changing, which requires continuous practice. Generally speaking, practice makes perfect. Summarizing the style of questioning over the years, I found that Lanqiao's question maker mainly investigated the enumeration algorithm in three aspects.

1. Simulation questions
This mainly examines basic knowledge points, like some binary conversion to decimal, running training, etc. In general, you simulate according to the requirements of the question, operate on the variables, and get the result
. 2.
Check your math questions Basic mathematics ability, don't worry, the requirements for mathematics ability are not very high, and the high school level can handle it.
3. Date questions.
This type of question should be a routine question. You must be able to handle the relationship between the year, month and day in the date. This type of question should be done two or three times, plus carefulness. The excel boss can skip it.

Study suggestions

  1. 100 questions for the basics of programming: If you feel that your basic knowledge is not very solid, you can pass all of the 100 questions, which will improve your own strength a lot. It is aimed at Xiaobai. Link: 100 questions for intro to programming
  2. Euler Project: specifically to cultivate students' mathematical thinking and learn to use mathematical methods to solve problems. Link: Project Euler
  3. Blue Bridge Cup Real Test: You can get the real test after finishing the above. Link: Real Blue Bridge Cup

Learning Resources

I have collected some learning resources on the Internet. Since the links are easy to fail, please follow the official account to mention them. The resources are collected on the Internet, please do not use them for business.
Insert picture description here

Guess you like

Origin blog.csdn.net/kiwi_berrys/article/details/113094786
Recommended