[PYTHON] AtCoder Beginner Contest 083 Review of past questions

Time required

スクリーンショット 2020-03-27 12.10.48.png

Impressions

Hmmm. I can't help but worry about FX. It's so bad that I can't study because I'm too worried about the market price ... I can't think of a solution, but my mental strength isn't enough.

Problem A

All you have to do is compare the added ones. I used the ternary operator.

answerA.py


a,b,c,d=map(int,input().split())
print("Left" if (a+b)>(c+d) else "Right" if (a+b)<(c+d) else "Balanced")

B problem

You can count from 1 to n in order.

answerB.py


n,a,b=map(int,input().split())
ans=0
for i in range(1,n+1):
    s=0
    j=i
    while j!=0:
        s+=(j%10)
        j//=10
    if a<=s<=b:ans+=i
print(ans)

C problem

When k was used as it was, it probably dropped by one case due to a calculation error. It is natural that non-integer calculations such as log calculations should be careful about errors **, but I want to keep in mind. Also, the second code is carefully written with the intention that it can be passed by O (log (x / y)).

answerC.py


import math
x,y=map(int,input().split())
k=math.floor(math.log2(y/x))

for i in range(k+3):
    if x*(2**i)>y:
        print(i)
        break

answerC_better.py


import math
x,y=map(int,input().split())
ans=0
while x<=y:
    x*=2
    ans+=1
print(ans)

D problem

At first, I looked at the sample examples and decided that there were a lot of 0s and 1s, but it wasn't that easy. In such an inversion problem, you can see that ** the relationship is reversed between the selected part and the unselected part in the inversion operation **. In other words, when [l, r] is inverted, the length of the character string is n, and when considering 1-index, the two parts [1, l-1], [r + 1, n] and [ The relationship of the l, r] part changes. Therefore, where 0s and 1s are swapped, they must be the same next to each other. Here, if the swapped location is k, k + 1, ** you need to select [v, k], [k + 1, w] and invert to make the two the same ** .. Here, I want to make the part to be selected from the subject as long as possible, and the length is obviously when v = 1 or w = n. Therefore, find max (length of [1, k], length of [k + 1, n]) for the places where they are replaced, and the shortest one is the answer. Also note that ** if they are all the same, the answer will be n **. (I had a pain because I had set inf to ans properly.)

answerD.py


s=input()
l=len(s)
ans=l
for i in range(l-1):
    if s[i]!=s[i+1]:
        ne=max(i+1,l-i-1)
        ans=min(ans,ne)
print(ans)

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions