I will explain how to solve ** A, B, C problem ** of ** AtCoder Beginners Contest 164 ** with ** Python 3 ** as carefully as possible.
I am aiming to explain a solution that satisfies the following three points, not just a method that can be solved.
--Simple: You don't have to think about anything extra --Easy to implement: I'm glad that mistakes and bugs are reduced ――It doesn't take long: The performance goes up and the time left for the D problem increases.
ABC164
Date: 2020-04-26 (Sun) 21:00 ~ 2020-04-26 (Sun) 22:40 (100 minutes) A Number of people submitting questions: 11236
Performance | AC | time | Ranking | Guideline |
---|---|---|---|---|
400 | ABC- | 29 minutes | 7198th | Tea performance |
600 | ABC- | 18 minutes | 5854th | Brown rate at 8 times |
800 | ABC- | 12 minutes | 4425th | Green performance |
1200 | ABC- | 5 minutes | 2081 | Water performance |
(Reference) I: Performance 1625 ABCD-- 23:03 80 5th place </ font>
The D problem was quite difficult, so just solving the C problem quickly gave me a water performance. I think you felt the importance of doing it in a fast and easy way.
ABC158A 『Sheep and Wolves』
** Problem page **: A --Sheep and Wolves ** Difficulty **: ☆☆☆☆☆ ** Point **: Handling of if statements, handling of boundary values
It's a simple matter of doing exactly as you write it.
'Unsafe'
when the number of wolves is ** "greater than or equal to" **, otherwise it is 'safe'
.
When judging by ʻif, be careful about handling the boundary value (the number of the boundary where the judgment changes). ** "More than" **, so
w> = s. If the number of wolves is ** "more" ** than the number of sheep, then
w> s`.
Kindly, sample 3 is 'unsafe'
with $ W = S $, so if this passes, you can submit with confidence.
Just do it. Be careful not to reverse ʻunsafe and
safe`, or make typos or spelling mistakes. One mistake can result in a penalty of 5 minutes, so even if it takes some time, be sure to check that it fits all the samples before submitting.
s, w = list(map(int, input().split()))
if w >= s:
print("unsafe")
else:
print("safe")
If you want to write in one line, you can use "ternary operator". I don't use it because it seems to cause mistakes.
s, w = list(map(int, input().split()))
print("unsafe") if w >= s else print("safe")
ABC164B『Battle』
** Problem page **: B --Battle ** Difficulty **: ★ ☆☆☆☆ ** Point **: Handling while, devising variable names
It is a simple game that you can reduce the opponent's HP by the same amount as your own attack power when you attack. Takahashi is the first player and Aoki is the second player.
Since $ A, B, C, D $ are all 100 or less, it can be solved by simulating as it is. If it is a very huge number such as 5000 trillion (dead language), it will be calculated by arithmetic. (It doesn't really matter, but there is actually a problem with 5000 trillion in the problem statement)
If the variable name is changed to ʻa,
b,
c,
d` as it is, it will be difficult to understand which variable is what and it will be a source of confusion. I was confused during the contest and ended up reading the question sentence about 5 times.
Let's say tkhp
, tkat
, ʻaohp, ʻaoat
.
Just write this too. For such a simulation, use while True
to make an infinite loop, and if the conditions are met, use break
to exit.
tkhp, tkat, aohp, aoat = list(map(int, input().split()))
while True:
aohp -= tkat
if aohp <= 0:
print("Yes")
break
tkhp -= aoat
if tkhp <= 0:
print("No")
break
By the way, if you leave the variable name as the problem statement, it will be the code below. It's a code that seems to induce mistakes. You can see the importance of variable names! (Self-discipline)
while True:
c -= b
if c <= 0:
print("Yes")
break
a -= d
if a <= 0:
print("No")
break
ABC164C『gacha』
** Problem page **: C --gacha ** Difficulty **: ★★ ☆☆☆ ** Type **: Utilization of set type
You can easily understand the meaning of the problem statement. The fun depends on how you implement it.
There are various ways to find the number of types, but the set type is the easiest.
If you normally receive the input as a list type and then make it a set type, the duplicate elements (double) will disappear. Since the number of elements of set type is the number of types itself, you can find the answer by using the len ()
function.
I don't think you can understand it even if it is said in words. There is nothing difficult if you look at what will happen.
>>> s = ['apple', 'orange', 'apple']
>>> s_set = set(s)
>>> s_set
{'orange', 'apple'} #There are two'apple'Became one
>>> len(s_set)
2 # 'orange'When'apple'There are two types.
>>> s = ['grape', 'grape', 'grape', 'grape', 'grape']
>>> s_set = set(s)
>>> s_set
{'grape'} #There are 5'grape'Became one
>>> len(s_set)
1 # 'grape'It is one type of.
n = int(input())
s = [input() for _ in range(n)] #I'm receiving input in a list comprehension that looks neat. The same is true with the for loop.
ss = set(s) #Create a set type of s. This is a bad variable name, but it's a short code so I think it's fine.
ans = len(ss) #Now you can see the number of types.
print(ans)
Excluding input, there are only 3 lines. If you use print (len (set (s)))
, it will be one line, but if you are not used to it, try to do only one line per line.
The technique of converting to set type and finding the number of types with len
is really often used, so be sure to remember it.
It's not relevant to this issue, but you often want to know how often each element appears.
In such a case, it is easier to write from collections import Counter
and use the Counter type. I use it as often as the set type.
I will not explain how to use it this time, so if you want to know it, please check it out. To briefly explain what it looks like, it is a convenient counter with various functions added to the dict type.
Recommended Posts