The 11th Lanqiao Cup Youth Group Provincial Competition Python Intermediate and Advanced Group Real Questions and Appreciation

The best way to practice is to practice. Use real questions to do it, not to analyze but to appreciate. Looking at it with appreciation, the questions are not only not difficult, but actually add a lot of fun. Next, the mystery of the python programming questions of the 11th Lanqiao Cup Youth Group Provincial Competition will be unveiled. Let’s appreciate them one by one to see if they are difficult or not.

Multiple choice questions

The multiple-choice questions are relatively basic and simple, and they are scored.

The answer is A. There is no so-called char type in programming languages. Although there is the concept of character, there is no separate character type. Generally, a character string such as 'a' can be used to represent a character.

The answer is D. 

 

The answer is D.​ 

The answer is C. The list type cannot be used as the key of the dictionary, but the other ones can.​ 

 This answer is A, the first index of "LIST".

Programming Question 1

Input a string. If the string ends with er, Iy or ing suffix, delete the string suffix and output the deleted string. Otherwise, output the original string.

Enter description
Enter a string

Output description
Output the deleted string or original string

Example import:
driver
Example export:
drive

Topic Appreciation 

The first question is usually a score question and is the easiest. This one is no exception. It simply processes the string and removes the specified special characters at the end. So the solution is simple:

# encoding: utf-8
# author:yangyongzhen
# blog.csdn.net/qq8864

n = input()

if n[-2:]=='er' or n[-2:]=='ly':
    print(n[:-2])
elif n[-3:]=='ing':
    print(n[:-3])
else:
    print(n)

#或者
if n.endswith('er'):
    print(n[:-2])
elif n.endswith('ly'):
    print(n[:-2])
elif n.endswith('ing'):
    print(n[:-3])
else: 
    print(n) 

Written in the form of a small function, it can be like this:

# encoding: utf-8
# author:yangyongzhen
# blog.csdn.net/qq8864

n = input()

def remove_suffix(str1):
  if str1.endswith('er'):
    return str1[:-2]
  elif str1.endswith('ly'):
    return str1[:-2]
  elif str1.endswith('ing'):
    return str1[:-3]
  else:
    return str1

out = remove_suffix(n)

print(out)

Programming Question 2

Factor, factor is also called factor, for example, 3*5=15, then 3 and 5 are factors of 15. At the same time, 15*1=15, then 1 and 15 are also factors of 15. The four factors 1, 3, 5, and 15 are all factors of 15.

Perfect number: If a number is equal to the sum of other factors excluding itself, then the number is called a "perfect number". For example, the factors of 6 are 1, 2, 3, 6, and 1+2+3=6, Therefore 6 is the perfect number.

Programming implementation

Input a positive integer N, and output all perfect numbers and numbers less than N (add * before the number, for example: *2).

Enter description

Enter a positive integer N

Output description

Output all perfect numbers and their numbers less than N

Sample input:

100

Example:
6
28
*2 

Appreciation of the topic

This question is not difficult, it is actually a simple math problem. It is very common to use programming to solve mathematical problems. If you understand how the mathematical principles of this problem are reflected in programming (that is, modeling, using mathematical language to facilitate computer processing by establishing a certain model), the implementation will be simple. For example, how to find the factors of a number? Since the factor can definitely be divisible, you can use the % remainder operator to see if the remainder is 0. If it is 0, the divisor must be a factor of this number. Also, the loop variable j will not exceed half of i, thus avoiding the need to calculate all factors of i, so there is no need to traverse from beginning to end. In this way, all the factors of this number can be obtained by traversing it once. The following code uses a sum1 to record the sum of all factors.

# encoding: utf-8
# author:yangyongzhen

n = input()

n = int(n)

count = 0

for i in range(2,n):
    sum1 = 0
    for j in range(1,i//2+1):
        if i%j == 0:
            sum1 += j
    if sum1 == i:
        print(i)
        count += 1

print('*' + str(count))

i//2 represents the integer part of i divided by 2 (the arithmetic operator "//" in python2.2 and later represents integer division, returning the largest integer not greater than the result. And "/" simply represents floating point division). In this code, i//2 is used to calculate half of i, which is then used as the maximum value of the loop variable j. This is done to ensure that the loop variable j does not exceed half of i, thus avoiding the need to calculate all factors of i. Additionally, sum1 is used to calculate the sum of all factors of i. i is a perfect number when the sum of all its factors equals i. Therefore, this code prints i when the sum of all factors of i equals i.

Programming Question 3

Problem description:
The user inputs a positive a as the diameter of the semicircle as shown in the figure, and as the length of the right side of an isosceles right triangle such as a circle, find the shaded part of the figure below Area .

Input description
A positive integer (as the diameter of the semicircle)
Output description
Shaded area of ​​the figure (reserved 2 decimal places)
Sample input:
10
Sample output
25.00< /span>

Topic Appreciation 

This question is not so much a programming question as it is a mathematics or geometry question. Why is it necessary to learn mathematics well to benefit programming? Because programming is also logical thinking, most of the problems encountered in the real world require the use of mathematics, and then computer modeling to design corresponding algorithms to solve the problems.

Triangular area formula: S= (ah) /2 (a in the formula is the base of the triangle, h is the height corresponding to the base).

The formula for the area of ​​a circle: S=n*r^2 (r in the formula is the radius of the circle, n=3.14 (pi)).

The above geometry question can be seen at a glance. The shaded part is obviously half the area of ​​the triangle. So the job of coding is the easiest.

(Pay attention to the question requirements and keep two decimal places)

# encoding: utf-8
# author:yangyongzhen

a = input()

a = int(a)

s = 0

s =  a*a/4

print("%.2f" %s)

Programming Question 4

In life, in order to ensure information security, we will encrypt important information before sending it to the other party. The other party needs to decrypt the information through the same strategy to understand the transmitted information. We call the information before encryption the original text and the information after encryption the ciphertext.

There is such an encryption strategy: the first encryption strategy is to encrypt the letter a in the plaintext as 01, b as 02, c as 03, and so on, x is encrypted as 24, y is encrypted as 25, z is encrypted as 26. Spaces are not encrypted. Then a second encryption is performed on the base of the first encryption. The second encryption is to encrypt the number 0 to 27, the number 1 to 28, and so on. The number 8 is encrypted to 35, and the number 9 is encrypted to 36. Spaces are encrypted as 00.

Programming implementation
Please enter a piece of plain text and output the corresponding cipher text according to the above encryption strategy.

Enter description

Enter a plain text (only lowercase letters and spaces)

Output description

According to the above encryption strategy, output the corresponding ciphertext

Sample input:

ab c
Sample output:

27282729002730 

Appreciation of the topic

This is already the fourth question, and it’s not difficult to find out. In fact, it is a simple replacement. Similar to looking up a dictionary (cipher text manual), you can find the corresponding ones and replace them as required. Therefore, the solution is as follows:

First design a dictionary. The design dictionary can be written out by hand. For example, the design dictionary d = {'a':'01','b':'02 39;}, but this is a bit cumbersome. To directly find the pattern, you can use a loop to output the dictionary. How to design the output dictionary? It requires some basic knowledge of characters. The decimal corresponding to the lowercase letters a, b, c, d,...z is 97, 98, 99....

In the python interpreter, you can directly enter the chr() function to view the ascii letter value corresponding to the number, or you can use the ord() function to enter the character to view its corresponding decimal value. python3 built-in functions hex(), oct(), bin(), chr(), ord().

chr() converts Ascii values ​​into corresponding characters, and the corresponding ord() function converts characters into values.

Therefore, the dictionary design can use a for loop, the design is as follows:

#设计明文对应的密文字典
d = {}

for i in range(1,27):
    #小于10的前面补个0凑够两位
    if i < 10:
        d[chr(i+96)] = '0'+str(i)
    else:
        d[chr(i+96)] = str(i)

#打印出字典
print(d)

The full answer is as follows:

# encoding: utf-8
# author:yangyongzhen

#设计明文对应的密文字典
d = {}

for i in range(1,27):
    #小于10的前面补个0凑够两位
    if i < 10:
        d[chr(i+96)] = '0'+str(i)
    else:
        d[chr(i+96)] = str(i)

#打印出字典
print(d)

#输入明文内容
ss = input()

s1 = ''

#遍历明文
for c in ss:
    #空格不加密
    if c ==' ':
       s1+= ' ' 
    else:
        s1 += d[c]

#至此完成了要求的一级加密
print(s1)
s2 = ''
for i in s1:
    if i == ' ':
        s2 += '00'
    else:
        s2 += str(int(i)+27)
        
#输出最终结果     
print(s2)

 Result output

Programming Question 5

The "Twenty-Four Solar Terms" are included in the UNESCO Human Intangible Cultural Heritage List. In the international meteorological community, this thousand-year-old time recognition system is known as "China's fifth greatest invention."

The spring rain shocks the spring and clears the valley sky, and the summer is full of awns and the summer heat is connected. There is dew in autumn, cold frost in autumn, and snow in winter and slight severe cold in winter. The twenty-four solar terms circulate in the four seasons, and each solar term has its relatively stable purpose.

The figure below gives the names of the twenty-four solar terms between January 25, 2020 and January 20, 2021 in the Gregorian calendar, the specific dates in the Gregorian calendar and the abbreviations of Chinese Pinyin.

Enter description

Enter a date between January 25, 2020 and January 20, 2021. For example: May 2, 2020 is written as "2020*05*02"

Output description

If the day happens to be a solar term, output the Chinese Pinyin abbreviation of this solar term. If the day is not a solar term, output the Chinese Pinyin abbreviation of the next solar term.

Sample input 1:

2020*06*21

Sample output 1:

XZ

Sample output 1 description: June 21 is the summer solstice, and the Chinese Pinyin abbreviation is "XZ"

Sample input 2:

2020*07*04

Sample output 2:

XS

Example output 2 explains: The solar term after July 4th is Xiaoshu, and the Chinese pinyin abbreviation is XS

Appreciation of the topic

There are many ways to solve this problem, as long as the goal is achieved. Although the description of this topic is relatively long, it actually examines basic search and comparison, mainly traversal and comparison. It is necessary to turn the text description into actual programming to find the answer. Two solutions are given here.

# encoding: utf-8
# author:yangyongzhen
# blog.csdn.net/qq8864

#names 列表,其中包含了所有节气的汉语拼音缩写
names = ['XH','DH' 'LC','YS','JZ', 'CF', 'OM', 'GY','LX','XM','MZ','XZ','XS','DS','LQ', 'CS','BL', 'QF','HL','SJ' ,'LD','XX','DX','DZ']

#dates列表,其中包含了所有节气的日期。
#这里把日期进行了抽象,实际105可不是1月5号。但用105来表示也不是不可以,方便比较大小来解题即可。
dates = [105,120,204,219,305,320,404,419,505,520,605,621,706,722,807,822,907,922,1008,1023,1107,1122,1207,1221]

#处理输入,把*替换掉并只取后面的日期
day = int(input().replace('*','')[-4:])

#输出日期
print(day)

dates_= [ i for i in dates if i < day]

print(dates_)

if len(dates_)==24:
    print('XH')
else :
    print(names[len(dates_)-1])

Use the replace() function to replace * in the input date with an empty string and only take the following date. Convert the input date to an integer using the int() function. Then use the dates_= [ i for i in dates if i < day] statement to create a new list, which contains all solar terms dates that are less than the input date.

Then use the len() function to calculate the length of the new list. If the length of the new list is 24, then it will output "XH" because this is the first period. Otherwise, output names[len(dates_)-1], where names[len(dates_)-1] is the Chinese Pinyin abbreviation of the last solar term in the new list. Because the question requires that if the day is not a solar term, then output the Chinese pinyin abbreviation of the next solar term. dates_ contains all solar terms that are less than the given date, then the natural next solar term of dates_ is names[len(dates_)-1] . Note that the index starts from 0.

This code can be used to calculate solar terms for any date. For example, if the input date is "2020*06*21", then the code will output "XZ" because June 21 is the summer solstice and the summer solstice is the 21st solar term. If the input date is "2020*07*04", then the code will output "XS" because July 4th is Xiaoshu, which is the 22nd solar term. If the input date is "2020*07*22", then the code will output "XZ" because July 22 is the summer solstice and the summer solstice is the 21st solar term.​  

Method Two:

# encoding: utf-8
# author:yangyongzhen
# blog.csdn.net/qq8864

def get_solar_term(year, month, day):
  """
  获取指定日期的节气

  Args:
    year: 年份
    month: 月份
    day: 日期

  Returns:
    节气的汉语拼音缩写
  """

  # 获取节气表
  solar_term_table = [
      ('小寒', 'XX', '2020-01-05'),
      ('大寒', 'DH', '2020-01-20'),
      ('立春', 'LC', '2020-02-04'),
      ('雨水', 'YS', '2020-02-19'),
      ('惊蛰', 'JX', '2020-03-05'),
      ('春分', 'CF', '2020-03-20'),
      ('清明', 'QM', '2020-04-04'),
      ('谷雨', 'GY', '2020-04-19'),
      ('立夏', 'LX', '2020-05-05'),
      ('小满', 'XM', '2020-05-20'),
      ('芒种', 'MZ', '2020-06-05'),
      ('夏至', 'XZ', '2020-06-21'),
      ('小暑', 'XS', '2020-07-06'),
      ('大暑', 'DS', '2020-07-22'),
      ('立秋', 'LQ', '2020-08-07'),
      ('处暑', 'CX', '2020-08-22'),
      ('白露', 'BL', '2020-09-07'),
      ('秋分', 'QF', '2020-09-22'),
      ('寒露', 'HL', '2020-10-08'),
      ('霜降', 'SG', '2020-10-23'),
      ('立冬', 'LD', '2020-11-07'),
      ('小雪', 'XS', '2020-11-22'),
      ('大雪', 'DS', '2020-12-07'),
      ('冬至', 'DZ', '2020-12-21'),
  ]

  # 遍历节气表,找到第一个日期大于等于输入日期的节气
  inpt = '%02d'%(year) + '-' + '%02d'%(month) + '-' + '%02d'%(day)
  print(inpt)
  for solar_term in solar_term_table:
    if solar_term[2] >= inpt:
        print(solar_term[2])
        return solar_term[1]

  # 没有找到节气,返回空字符串
  return ''


def main():
  # 获取输入日期
  year, month, day = input().split('*')

  # 获取节气
  solar_term = get_solar_term(int(year), int(month), int(day))

  # 输出节气
  print(solar_term)


if __name__ == '__main__':
  main()

 Enter 2020*07*04 and the result is XS. Input 2020*06*21, and the output result is XZ.

Summarize

After analyzing a set of Lanqiao Cup test questions, I felt that it was really not difficult and quite simple. The Lanqiao Cup Youth Group Provincial Competition Python Intermediate/Advanced Group Programming Questions, even the last big question, except for the lengthy text description, is not difficult to implement. Once you understand the meaning of the question, just convert it into programming implementation. Maybe this is aimed at beginners and teenagers, it's a bit too easy for programmers. Even for teenagers, as long as you have a solid foundation, you can do it all.

The difficulty of the questions is similar to that of the Blue Bridge Cup National Competition/Advanced Group. It mainly tests the basic knowledge of Python programming, including data types, variables, operators, functions, loops, conditional judgments, etc. Some common programming question types will also be examined, such as string processing, array processing, sorting, search, recursion, etc. The difficulty is not too difficult. As long as you master the basic knowledge of Python programming, be familiar with common programming question types, and have a solid foundation in mathematics, you can successfully pass the exam and get full marks.

Other resources

The representation and conversion methods of numbers in different bases in Python, the use of some functions (detailed explanation of hex, oct, bin functions, detailed explanation of the base conversion calculation process)_base function python_Times Wait 198's Blog-CSDN Blog

Python outputs the ascii code of characters_mob64ca12e83232's technical blog_51CTO blog

python3 built-in functions hex(), oct(), bin(), chr(), ord()_yyy9331's blog-CSDN blog

GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++

Guess you like

Origin blog.csdn.net/qq8864/article/details/134751542