[PYTHON] AtCoderBeginnerContest168 Review & Summary (first half)

AtCoder ABC168 This is a summary of the AtCoder Beginner Contest 168 questions that were held on 2020-05-17 (Sun), starting with question A and taking into consideration the considerations. The first half deals with problems up to ABC. The problem is quoted, but please check the contest page for details. Click here for the contest page Official commentary PDF

Problem A ∴ (Therefore)

Problem statement Iroha decided to teach Japanese for her cat, Sunuke, who wants to play the popular Japanese game "Åt Coder". When counting pencils in Japanese, "book" is added after the number as a classifier. This classifier is read differently depending on what number it is attached to. Specifically, for a positive integer $ N $ less than $ 999 , the reading of "book" when saying " N $ book" is ・ "Hon" when the $ 1 $ place of $ N $ is $ 2,4,5,7,9 $ ・ When the $ 1 $ place of $ N $ is $ 0,1,6,8 $, "pon" ・ "Bon" when the $ 1 $ place of $ N $ is $ 3 $ . $ N $ is given, so output the reading of the "book" when you say "$ N $ book".

The input is received as a number, and the remainder after dividing by $ 10 $ is in the order of $ 1 $. I think it is an error, but in order to submit it as soon as possible, I set the output of "hon" to ʻelse` (the conditional expression writing becomes a little shorter).

abc168a.py


n = int(input())
k = n % 10
if k == 3:
    print("bon")
elif k == 0 or k == 1 or k == 6 or k == 8:
    print("pon")
else:
    print("hon")

Problem B ... (Triple Dots)

Problem statement There is a string $ S $ consisting of lowercase letters. If the length of $ S $ is less than $ K $, output $ S $ as it is. If the length of $ S $ is longer than $ K $, cut out only the first $ K $ character and add ... at the end to output.

For the time being, compare the length of $ S $ and $ K $ with an if statement, and write the processing when it exceeds the following and the end. Since python can handle strings like arrays, I wrote it without using a for statement.

abc168b.py


n = int(input())
s = input()
if len(s) <= n:
    print(s)
else:
    print(s[:n] + "...")

Problem B was solved in a few minutes from the start of the contest, so I was able to solve it in my ideal time.

Problem C: (Colon)

Problem statement Consider an analog clock whose hour and minute hands are $ A $ centimeter and $ B $ centimeter, respectively. One end point of each of the hour hand and the minute hand is fixed at the same fixed point, and each hand rotates clockwise at a constant angular velocity around this point. The hour hand is $ 12 $ hour and the minute hand is $ 1 $ hour. At $ 0 $, the hour and minute hands overlapped. How many centimeters are the unfixed endpoints of the $ 2 $ hands apart when they reach just $ H $ hours and $ M $ minutes?

I came up with a few ways to solve it, but I solved it using the cosine theorem. The value $ D $ you want to find is

D = \sqrt{A^2 + B^2 - 2AB\cos \theta}

Since it can be calculated with, it can be solved by calculating $ \ theta $. If the angles of the hour and minute hands from 0 o'clock to the hands clockwise are $ \ theta_A and \ theta_B $, respectively.

\theta_A = 30 \times H + 0.5 \times M \\
\theta_B = 6 \times M

Therefore, the angle $ \ theta $ formed by the hour and minute hands is

\theta = \theta_A - \theta_B

You can find it with. Here, it is possible that $ \ theta $ is greater than $ 180 $ degrees or negative.

\cos (360 - x) = \cos x \\
\cos (-x) = \cos x

Therefore, the length can be calculated by calculating the cosine theorem without any problem.

abc168c.py


import math
a, b, h, m = map(int, input().split())
x = 30 * h + m / 2 - 6 * m
d = math.sqrt(a**2 + b**2 - 2 * a * b * math.cos(math.radians(x)))
print(d)

Another thing I came up with was $ (A_x, A_y) = (A \ cos \ theta_1, A \ sin \ theta_1), (B_x, B_y) = (B \ cos \ theta_2, B) for the coordinates pointed to by the hour and minute hands, respectively. \ sin \ theta_2) $, and $ D $ you want to find by using the trigonometric theorem is

D = \sqrt{(A_x - B_x)^2 + (A_y - B_y)^2}

You can find it with.

I used the law of cosines because the answer is a positive real number and there are restrictions on the absolute or relative error with the correct value, so I didn't know how much error would occur in the calculation of math, so I used it as much as possible. It was solved by the cosine theorem that can reduce the number of uses.

This is the end of the first half. Thank you for reading to the end of the first half.

The second half will explain the DEF problem. Continued in the second half.

Recommended Posts

AtCoderBeginnerContest175 Review & Summary (first half)
AtCoderBeginnerContest164 Review & Summary (first half)
AtCoderBeginnerContest169 Review & Summary (first half)
AtCoderBeginnerContest174 Review & Summary (first half)
AtCoderBeginnerContest173 Review & Summary (First Half)
AtCoderBeginnerContest170 Review & Summary (first half)
AtCoderBeginnerContest167 Review & Summary (first half)
AtCoderBeginnerContest177 Review & Summary (first half)
AtCoderBeginnerContest168 Review & Summary (first half)
AtCoderBeginnerContest178 Review & Summary (first half)
AtCoderBeginnerContest171 Review & Summary (first half)
AtCoderBeginnerContest166 Review & Summary (first half)
AtCoderBeginnerContest161 Review & Summary (first half)
AtCoderBeginnerContest172 Review & Summary (first half)
AtCoderBeginnerContest176 Review & Summary (first half)
AtCoderBeginnerContest178 Review & Summary (second half)
AtCoderBeginnerContest161 Review & Summary (second half)
AtCoderBeginnerContest164 Review & Summary (second half)
AtCoderBeginnerContest176 Review & Summary (second half)
AtCoderBeginnerContest168 Review & Summary (second half)
AtCoderBeginnerContest169 Review & Summary (second half)
AtCoderBeginnerContest166 Review & Summary (second half)
AtCoderBeginnerContest174 Review & Summary (second half)
AtCoderBeginnerContest173 Review & Summary (second half)
AtCoderBeginnerContest177 Review & Summary (second half)
AtCoderBeginnerContest180 Review & Summary
AtCoderBeginnerContest181 Review & Summary
AtCoderBeginnerContest182 Review & Summary
AtCoderBeginnerContest183 Review & Summary
AtCoderBeginnerContest179 Review & Summary
Django Girls Tutorial Summary First Half
AtCoder Review of past questions (first half of 12 / 8,9)