0
0

More than 1 year has passed since last update.

AtCoder Beginners Selection 11問をpythonで解いてみた

Last updated at Posted at 2022-01-17

AtCoderの初心者向け問題集である AtCoder Beginners Selection の11問をPythonで解説します。

Welcome to AtCoder

PracticeA
a = int(input())
b, c = map(int, input().split())
s = input()
print(a+b+c, s)

【解説】簡単な入力の問題です。
aは後に計算するため、input関数をint関数の引数に設定し、整数値として入力を受け取ります。
複数の文字の入力にはmap関数を使用し、第一引数にint関数を設定することで、整数値としてb,cに設定します。
sは文字列として受け取るので、input関数です。表示にはprint関数を使用し、a,b,cの合計値と、sを表示します。

詳しい解説はこちら

Product

ABC086A
a, b = map(int, input().split())
ans = a * b
if ans % 2 == 0:
    print('Even')
else:
    print('Odd')

【解説】map関数を使用し2つの整数値の入力を行い、if文での条件式で偶奇の判断を行います。偶数か奇数かを判定するためにa,bの積を 2 で割った余りが 0 か 1 かで判断し、偶数であれば'Even'、奇数であれば'Odd'を出力します。

詳しい解説はこちら

Placing Marbles

ABC081A
s = input()
print(s.count('1'))

【解説】input関数を使用し、文字列として数字を受け取り、countメソッドで'1'の数を数えます。

詳しい解説はこちら

Shift only

ABC081B
N = int(input())
A = list(map(int, input().split()))

flag = 0
count = 0

while True:
    for i in range(N):
        if A[i] % 2 != 0:
            flag = 1
    if flag == 1:
        break
    for i in range(N):
        A[i] = A[i]//2
    count += 1
print(count)

【解説】終了判定のfor文と、リストの値を2で割ったものをリストの再設定するfor文に分けます。終了判定のfor文では、割り切れない場合にflagに1を設定し、if文でflagに1が設定されている場合に、break文でwhile文を終了します。リストの値を2で割ったものをリストの再設定するfor文ではリストから1つずつ値を取り出し、2で割ったものをリストに再設定し、カウントします。print関数でこのカウント数を表示します。

詳しい解説はこちら

Coins

ABC087B
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count = 0

for i in range(A+1):
    for j in range(B+1):
        for k in range(C+1):
            if 500*i + 100*j + 50*k == X:
                count += 1
print(count)

【解説】for文を入れ子にし、全ての枚数の組み合わせを作成します。一番内側のfor文内でそれぞれの枚数(i,j,k)と硬貨の価値(500,100,50)の合計値が金額Xと等しくなる場合に、if文内のカウンタ変数countを+1します。for文終了後、print関数でこのカウント数を表示します。

詳しい解説はこちら

Some Sums

ABC083B
N, A, B = map(int, input().split())
count = 0

for i in range(N+1):
    if A <= sum(list(map(int, str(i)))) <= B:
        count += i
print(count)

【解説】for文内でmap関数を使用し、第二引数でstr関数で文字列となった数字を各桁で分断し、int型に変換、リストにします。その合計値がA以上B以下であればカウントします。for文終了後、print関数でこのカウント数を表示します。

詳しい解説はこちら

Card Game for Two

ABC088B
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
Alice_calds = a[0::2]
Bob_calds = a[1::2]
ans = sum(Alice_calds)-sum(Bob_calds)
print(ans)

詳しい解説はこちら

Kagami Mochi

ABC085B
N = int(input())
d = [input() for _ in range(N)]
print(len(set(d)))

詳しい解説はこちら

Otoshidama

ABC085C
N, Y = map(int, input().split())

for i in range(N+1):
    for j in range(N-i+1):
        k = N - i - j
        if 10000*i + 5000*j + 1000*k == Y:
            print(i, j, k)
            exit()
print(-1, -1, -1)

詳しい解説はこちら

白昼夢

ABC049C
s = input()
s = s.replace('eraser', '')
s = s.replace('erase', '')
s = s.replace('dreamer', '')
s = s.replace('dream', '')
if s:
  print('NO')
else:
  print('YES')

詳しい解説はこちら

Traveling

ABC086C
N = int(input())
t_old = 0
x_old = 0
y_old = 0
flag = True

for _ in range(N):
    t, x, y = map(int, input().split())
    distance = abs(x-x_old) + abs(y-y_old)
    time = t-t_old

    t_old = t
    x_old = x
    y_old = y

    if time < distance:
        flag = False
    elif time % 2 != distance % 2:
        flag = False

if flag:
    print('Yes')
else:
    print('No')

詳しい解説はこちら

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