Blue Bridge Cup Python Teens Zhenti_Blue Bridge Cup Python Zhenti Analysis

Today, I will analyze a real topic "The Number of Walnuts" from the Python team of the Blue Bridge Cup Competition.

Topic content: Xiao Zhang is a software project manager who leads 3 development teams. The schedule is tight, I am working overtime today. To boost morale, Zhang plans to give each group a bag of walnuts (it is rumored that walnuts can nourish the brain). His request is:

1. The number of walnuts in each group must be the same

2. Each group must be able to divide walnuts equally (of course they cannot be broken)

3. Try to provide the minimum quantity that satisfies the conditions of 1 and 2 (the economy is making a revolution)

The user inputs three positive integers a, b, c to represent the number of people working overtime in each group, separated by spaces (a, b, c<30), and the system automatically displays the number of walnuts in each bag according to the number input by the user .

Item analysis: When the number of three groups is determined, the number of walnuts in each bag is the least common multiple of the three groups.

Algorithm analysis: Generally, the problems we encounter are to find the least common multiple of two known numbers. Now we need to calculate the least common multiple of three numbers. There are many algorithms, such as decomposing prime factors, formula method and enumeration method. The enumeration method is the simplest and most suitable for computer use. Today, the enumeration method is written in Python to count the number of walnuts.

First, use the Python built-in function map(function, iterable...) to input the number of three development groups and store them in variables a, b, and c. In the built-in function, function represents the function name, iterable represents one or more sequences, and the input numbers should be separated by spaces.

Using the enumeration algorithm, set the initial number of walnuts num equal to 1, and set a T to control the loop condition of While; the default T is True. After the program runs, it enters the While loop for judgment. When num can divide a, b, and c at the same time, T is False to end the loop. Otherwise, the number of walnuts is increased by one until the final result is calculated. The program reference is shown in the figure.

In addition, please use the decomposing prime factor method and the formula method to find the least common multiple to write the program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324218909&siteId=291194637