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.
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")
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)
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)
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