0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC216 C - Many Balls から学んだ

Last updated at Posted at 2021-10-19

abc216c_1.png
abc216c_2.png
abc216c_3.png

分からないので解説確認。

なるほど。以下で通った。

abc216c.py
N = int(input())
ans = ""

while True:
    if N == 0:
        break
    elif N%2 == 0:
        ans += "B"
        N //= 2
    elif N%2 != 0:
        ans += "A"
        N -=1

print(ans[::-1])#23ms

2^120 は 10^36 らしいので、
N%2 を最優先に記述すれば
基本的には 120回以下に収まると判断していいはず

参加者の解答を眺めていたが、
while 分の条件を設定できれば
if / elif / else の記述が少し減らせるのは面白いと思った。

abc216c.py
def solv():
    N = int(input())
    ans = ""
    while N>1:
        if N%2 == 0:
            ans += "B"
            N //= 2
        else:
            ans += "A"
            N -= 1
    ans += "A"
    print(ans[::-1])
solv()#30ms
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?