LoginSignup
0
2

More than 3 years have passed since last update.

AtCoder Beginners Selection 備忘録

Last updated at Posted at 2020-02-19

 
プログラミング初心者なので、AtCoderのBeginners Selectionで勉強します。
使用言語はpythonです。

スクリーンショット 2020-02-19 14.35.08.png
自分の回答

ans1.py
a = int(input())
b,c = map(int,input().split())
s = input()

print(a+b+c,s)

スクリーンショット 2020-02-19 14.37.54.png
自分の回答

ans2.py
a,b = map(int, input().split())

if (a*b) % 2 == 0:
    print("Even")
else:
    print("Odd")
print(a+b+c,s)

スクリーンショット 2020-02-19 14.38.51.png
自分の回答

ans3.py
a = input()
l = [int(x) for x in list(str(a))]

sum = int(l[0])+int(l[1])+int(l[2])
print(sum)

このやり方は一般的ではないですね。。後に、l.count('1')というものを知りました。

スクリーンショット 2020-02-19 14.41.25.png
自分の回答

ans4.py
import numpy as np

N = input()
l = list(map(int, input().split()))
array = np.array(l)

i = 0

while sum(array % 2) == 0:
  array = array/2
  i = i++

print(i)

スクリーンショット 2020-02-19 14.44.12.png
自分の回答

ans5.py
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count = 0

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

print(count)

全探索?っていうのかな?

スクリーンショット 2020-02-19 14.46.27.png
自分の回答

ans6.py
import numpy as np

N,A,B = map(int,input().split())
count = 0
l2 = []

for i in range(1,N+1):
    l = np.array([int(x) for x in list(str(i))])
    if A <= np.sum(l) & np.sum(l) <= B:
        l2.append(i)

l3 = np.array(l2)
print(l3.sum())

変数名テキトーすぎますね。。

スクリーンショット 2020-02-19 14.49.28.png
自分の回答

ans6.py
import numpy as np

N = int(input())
a = np.array(list(map(int,input().split())))

sort = np.sort(a)[::-1]

alice = sort[::2]
Bob = sort[1::2]

print(alice.sum() - Bob.sum())

sortに救われています。

スクリーンショット 2020-02-19 15.09.19.png
自分の回答

ans7.py
import numpy as np

N = int(input())
l = []
count = 1

for i in range(N):
    l.append(int(input()))

array = np.sort(np.array(l))

for i in range(len(array)-1):
    if array[i] != array[i+1]:
        count = count + 1

print(count)

なんかスマートじゃない気もする。

スクリーンショット 2020-02-20 15.32.47.png
自分の回答①(誤答)

ans8_1.py
import sys

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

for i1 in range(N+1):
    for i2 in range(N+1-i1):
        for i3 in range(N+1-i1-i2):
            if 10000*i1 + 5000*i2 + 1000*i3 == Y:
                print(i1,i2,i3)
                sys.exit()
print(-1,-1,-1)

テストコードによって正解、不正解、実行時間超過という様々な結果が得られた。

自分の回答②(誤答)

ans8_2.py
N,Y = map(int,input().split())
flag = False

for i1 in range(N+1):
    for i2 in range(N+1-i1):
        for i3 in range(N+1-i1-i2):
            if 10000*i1 + 5000*i2 + 1000*i3 == Y:
                print(i1,i2,i3)
                flag = True
                break
        if flag:
            break
    if flag:
        break

if flag-1:
    print(-1,-1,-1)

ループを抜けるために、exit()ではなく、flag変数を導入した。これもテストコードによって正解、不正解、実行時間超過という様々な結果が得られた。何が不正解なのかわからないのでだれか助けてほしい!(→解決した。三つ目のforは要らないね!!)

自分の回答③(正答)

ans8_3.py
N,Y = map(int, input().split())
for i1 in range(N+1):
    for i2 in range(N+1-a):
        i3 = N-i1-i2
        if 10000*i1 + 5000*i2 + 1000*i3 == Y:
                print(i1,i2,i3)
                exit()
print(-1,-1,-1)

スクリーンショット 2020-02-21 13.05.57.png
自分の回答

ans9.py
S = input().replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')
if (len(S) == 0):
    print('YES')
else:
    print('NO')

文字列の置換replace()ですね。

0
2
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
2