Blue Bridge Cup Software Competition---Hand Calculation Guide

Precursor   textbook : " Introduction to Advanced Algorithm Competition" Tsinghua University Press
Online shopping : JD .


  In the current Blue Bridge Cup provincial competition, each competition has 10 questions, of which 5 are fill-in-the-blank and 5 are programming.
  Each competition has "send points", which can be done in just a few minutes. Especially for some fill-in-the-blank questions, you only need to fill in the answer without submitting the code, so you can use a variety of methods including coding. Coding is generally slow, so don't code if you can, but use reasoning and hand calculations to find the answer. This type of fill-in-the-blank problem without coding is called "hand calculation problem".
  The competition time is extremely tight, and the fastest implementation method should be selected.
  Here are 4 tips: use the editor skillfully, see the number of hands, use Excel skillfully, and use Python skillfully.

1. Use the editor skillfully

(1) House plate production


Item source: 2020 C++ Group A, Question A: House Number Production Item
Description: How many 2s are there in all the numbers from 1 to 2020?


  This is really a bonus question, and the coding is very simple: determine how many 2s are in each number, and then add up the number of 2s in all numbers. Coding takes about 5 minutes.
  But there is a simpler way: first encode and print out the 2020 numbers from 1 to 2020, then paste them into an editor (word, codeblocks), select the replacement function, replace '2' with a certain character, and replace a total of 624 times, this is the answer. Take 1 minute.
  First print the numbers with code (writing code in python is even shorter):

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
  int k=0;
  for(int i=1;i<=2020;i++)
     cout<<i;
}

  Print out the 2020 numbers:

  Paste the numbers back into codeblocks, replaced 624 times.

(2) The new title "card" for the 2021 provincial competition

Today (2021.4.18) Question 1 of Group A and Question 2 of Group B of the provincial competition:


Little Blue has many number cards, and each card has numbers 0 to 9. Xiaolan is going to use these cards to spell some numbers. He wants to spell positive integers from 1. After each spelling, he saves it, and the cards cannot be used to spell other numbers. Xiaolan wants to know how much he can fight from 1. For example, when Xiaolan has 30 cards, including 3 cards from 0 to 9, Xiaolan can spell 1 to 10, but when he spells 11, there is only one card 1, which is not enough to spell 11. Now Xiaolan has 2021 cards from 0 to 9 in his hand, a total of 20210 cards. How many cards can Xiaolan get from 1? Tip: It is recommended to use computer programming to solve the problem.


  Similar to the example above.
  It is estimated that more than 3000 numbers may be spelled out.
  Make a small program to print out 1~3500, then paste all of them into word, see how many times 1 is used, how many times 2 are used...
  Finally, 1~3181, 2021 1s are used, and so on. So the answer is 3181.
  Do it again with python later.

2. Look at the lot size

  Some fill-in-the-blank questions themselves are more complicated, but because the data is simple, no coding is needed at this time, just look at it with your eyes and count by hand.

(1) Maze


Item source: 2017 C++ Group A, Question 1: Maze Item
description: Given a maze, ask how many people in the maze can get out.
The maze is as follows: where L means go left, R means go right, U means go up, and D means go down.

UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR

  This question is a typical DFS with at least 10 minutes of coding. However, because it is a fill-in-the-blank question, and the maze is very simple, there are only 100 characters, you can count directly, from left to right, from top to bottom, and you can count it in about 2 minutes. The result of the count is shown below, and the person on the red character can come out.

(2) Seven-segment code


Title source: 2020 C++ Group A, Question D: Seven-segment code
Title description: Seven-segment digital tube, a total of 7 light-emitting diodes, ask how many different characters can be represented, and the light-emitting diodes are required to be connected.


  The problem requires that the light-emitting diodes are connected. You can use DFS or union search to find connected blocks, and the coding time is more than 15 minutes. However, because the graphics are simple, you can do it directly by hand, which takes about 3-5 minutes.
  It is not convenient to use characters to represent digital tubes, so use numbers instead:

  Divided into 7 situations:
  one light is on: there are 7 situations, 1, 2, 3, 4, 5, 6, 7;
  two lights are on: 12, 13, 23, 24, 25, ... and so on;
  three lights are on Lights: There are 123, 124, 125, 134, 136, 234, 257, etc.;
  when four lights are on, do not count the four lights directly. The situation is equivalent to turning off three lights: off 123, off 124 ...etc;
  turn on five lights, which is equivalent to turning off two lights: turn off 12, turn off 13, turn off 14, ... etc.;
  turn on six lights, which is equivalent to turning off one light, there are 7 situations;
  turn on seven Light, there is 1 case.
  Sum all the above cases.

3. Use Excel skillfully

(1) Digital calculation


Item source: 2018 C++ Group A, Item 1: Score Item
Description: 1/1 + 1/2 + 1/4 + 1/8 + 1/16 + …Each item is half of the previous item, if there are a total of 20 Item, find what this sum is, and the result is expressed as a fraction.


  Coding is easy and takes a few minutes. You can also use Excel to calculate by hand, the time is about the same, and there is no need to think.
  Fill in the numerator in column A, which is 1; fill in the denominator in column B, and increase by 2 times for each row. The method is to fill in 1 in B1, and fill in "=B1*2" in B2, and then press and hold B2 to pull down to the 20th row. Fill in all the denominators.
  Then find the numerator and denominator. The denominator is 524288 of B20, and the numerator is actually "SUM(B1:B20)". Select this area with the mouse, and Excel automatically calculates 1048575.

(2) Date problem


Title source: 2018 C++ Group A, Question 2: Monday
Title Description: How many Mondays were there in the entire 20th century (between January 1, 1901 and December 31, 2000)?


  In Excel, enter the date January 1, 1901 in one cell and December 31, 2000 in the other cell, then subtract the two cells to get 36524 days, and divide by 7 to get 5217.7 weeks.

Figure (1) Date subtraction

  Then use excel to point the attribute of December 31, 2000, select the week, and get "Sunday", indicating that the answer is 5217.

Figure (2) View date is the day of the week

4. Use Python skillfully

  Python is very simple to deal with numbers. When you encounter such a fill-in-the-blank problem, you can use Python.
  Even if you are participating in the C/C++ and Java group competitions, you must learn some Python to facilitate hand calculations.
  The code length of Python is generally much shorter than that of C/C++ and Java. For example, 30 lines of C++ code can be written in Python with only 20 lines.

(1) Use Python to calculate large numbers


Item source: 2018 C++ Group A, Question 3: Product Trailing Zeros Item
description: Give 100 integers and ask them how many zeros are at the end of their product.


  When encountering problems with large numbers, it is easiest to deal with them in Python, and you can directly calculate them.
  However, in fact, Python's large numbers are not infinite. In the following code, if you first calculate the product s of all 100 numbers, s is too large and will overflow. So when multiplying a number, it depends on whether there is a 0 after the product s. If there is a 0, divide it by 10, so that s is relatively small.
  Below is the code for the "product trailing zeros" problem.

#输入放在一行中,不要分10行
num=[int(i) for i in input().split()] 
# input().split()读一行以空格分开的元素,然后用int()转为整数
s = 1
cnt = 0
for i in range(len(num)):   #连续乘,一边乘一边统计0的个数
    s *= num[i]             #乘一个数
    while s%10 == 0:        #末尾是零
       s /= 10              #除以10,把末尾零去掉
       cnt += 1 
print(cnt)

(2) Processing characters in Python


Title source: 2019 C++ Group A, Question 1: Sum of Squares
Title Description: Xiao Ming is very interested in numbers that contain 2, 0, 1, and 9. Such numbers in 1 to 40
include 1, 2, and 9. , 10 to 32, 39 and 40, a total of 28, their sum is 574, the sum of squares is 14362.
Note that the sum of squares means that each number is squared separately and then summed.
Excuse me, what is the sum of the squares of all such numbers from 1 to 2019?


  Using Python, without any algorithm, count numbers directly as characters:

sum = 0
for i in range(1,2020):
    s = str(i)
    if '2' in s or '0' in s or '1' in s or '9' in s:
         sum += i*i
print(sum)

(3)


Item source: 2019 C++ Group A, Question 2: Sequence Evaluation Item
Description: Given a sequence of 1, 1, 1, 3, 5, 9, 17, …, starting from item 4, each item is the first 3 items and. Find
the last 4 digits of item 20190324.


  The following is the Python code, which is directly calculated, very simple:

a,b,c = 1,1,1
for i in range(4,20190325):
    y=(a+b+c)%10000
    a=b
    b=c
    c=y
print(y)

(3) New questions for the 2021 provincial competition

Today (2021.4.18) Question 1 of Group A and Question 2 of Group B of the provincial competition:


Little Blue has many number cards, and each card has numbers 0 to 9. Xiaolan is going to use these cards to spell some numbers. He wants to spell positive integers from 1. After each spelling, he saves it, and the cards cannot be used to spell other numbers. Xiaolan wants to know how much he can fight from 1. For example, when Xiaolan has 30 cards, including 3 cards from 0 to 9, Xiaolan can spell 1 to 10, but when he spells 11, there is only one card 1, which is not enough to spell 11. Now Xiaolan has 2021 cards from 0 to 9 in his hand, a total of 20210 cards. How many cards can Xiaolan get from 1? Tip: It is recommended to use computer programming to solve the problem.


Use Python to do it hard, simple and direct!
First print out 20210 characters, and then remove the corresponding card for each number spelled out.
The following are 3 codes:
(1) This is what I wrote, and later found it was too troublesome:

s =''                   #放20210个字符
for i in range(0,10):   #打印20210个字符
     for j in range(0,2021):
          s = str(i)+s

ans = 0                 #统计能拼的个数
for i in range(1,10000):     
     num = str(i)      #数字用字符表示
     len_card = len(num)   #这个数字有几位
     in_card = 1
     for j in range(len_card):
          if num[j] not in s:  #检查每个数字是不是在卡片里面
               in_card = 0       #这个数字拼不出来,结束
               print(ans)
               exit(0)
     if in_card == 1:    
          ans = ans+1
          for j in range(len_card):
               s=s.replace(num[j],"*",1)  #把用过的卡片去掉

Answer 3181

(2) The code of Lin Yueyang for chemical drugs 2017:

s=['0','1','2','3','4','5','6','7','8','9']*2021
for i in range(1,10000):
     a=list(str(i))
     try:
          for j in a:
               s.remove(j)
     except:
          print(i-1)
          break

(3) Photoelectric 181 Chen Tao's code. Because the number 1 is used the most, 2021 times are used when the number is counted, just fine.

s=""
for i in range(1,100000):
     s+=str(i)
     if s.count('1') == 2021:
          print (i)
          break

Guess you like

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