LoginSignup
7
5

More than 3 years have passed since last update.

Atcoderの過去問精選10問をpythonで解いてみた!

Last updated at Posted at 2020-02-06

はじめに

 Atcoder初心者です。練習のためにこの記事の問題(AtCoder に登録したら次にやること ~ これだけ解けば十分闘える!過去問精選 10 問 ~)をpythonで解いてみました。

第 1 問: ABC 086 A - Product

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

第 2 問: ABC 081 A - Placing Marbles

s = input()
count = 0

for i in s:
    if i=='1':
        count += 1

print(count)
別解1
print(input().count('1'))
別解2
print(int(input()) % 9)

第 3 問: ABC 081 B - Shift Only

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

#all(): 全ての要素がTrueならTrue
#any(): 一つでもTrueならTrue
while all(a%2==0 for a in A): 
    A = [a/2 for a in A]
    count += 1

print(count)

第 4 問: ABC 087 B - Coins

A = int(input())
B = int(input())
C = int(input())
X = int(input())
counts = 0

for a in range(A+1):
    for b in range(B+1):
        for c in range(C+1):
            if (500*a + 100*b + 50*c) == X:
                counts += 1

print(counts)
別解
A, B, C, X = [int(input()) for _ in range(4)]
print([500*a + 100*b + 50*c for c in range(C+1) for b in range(B+1) for a in range(A+1)].count(X))

第 5 問: ABC 083 B - Some Sums

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

def FindSumOfDigits(x):
    count = 0
    while x>0:
        count += x%10
        x = x//10

    return count

for n in range(1, N+1):
    count = FindSumOfDigits(n)
    if A<= count <=B:
        ans += n

print(ans) 

第 6 問: ABC 088 B - Card Game for Two

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
alice = bob = 0
for i in range(N):
    if i%2 == 0:
        alice += A[i]
    else:
        bob += A[i]

print(alice - bob)

第 7 問: ABC 085 B - Kagami Mochi

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

第 8 問: ABC 085 C - Otoshidama

N, Y = map(int, input().split())
res10000 =res5000 = res1000 = -1

for x in range(N+1):
    for y in range(N+1-x):
        z = N-x-y # zは決まっているので、ループを減らして、計算量を減らす!
        if 10000*x + 5000*y + 1000*z == Y:
            res10000 = x
            res5000 = y
            res1000 = z

print(res10000, res5000, res1000)

第 9 問: ABC 049 C - Daydream

import re
S = input()
'''
メタ文字:
^ = 文字列の先頭、
| = OR
+ = 直前のパターンを1回以上繰り返し、
$ = 文字列の末尾
'''
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print('YES')
else:
    print('NO')

第 10 問: ABC 086 C - Traveling

N = int(input())
t = [0] * (N+1)
x = [0] * (N+1)
y = [0] * (N+1)
for i in range(N):
    t[i+1], x[i+1], y[i+1] = map(int, input().split())

f = True
for i in range(N):
    dt = t[i+1] - t[i]
    dist = abs(x[i+1]-x[i]) + abs(y[i+1]-y[i])
    if dt < dist:
        f = False
    if dist%2 != dt%2:
        f = False

print('Yes' if f else 'No')
7
5
1

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
7
5