LoginSignup
0
0

More than 3 years have passed since last update.

はじめに

前回
Boot camp for Beginnersをやっていきます

#34

ABC075-B

考えたこと
forとifで置き換えていくだけ

h, w = map(int,input().split())
s = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '#':
            continue
        for n, m in ([-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]):
            if i + n < 0 or i + n >= h or j + m < 0 or j + m >= w:
                continue
            if s[i+n][j+m] == '#':
                count += 1
        s[i][j] = str(count)

ans = ''
for i in range(h):
    c = ''.join(s[i])
    ans += c
    if i != h-1:
        ans += '\n'
print(ans)

ABC098-B

考えたこと
全探索するだけ

n = int(input())
s = input()

ans = 0
for i in range(1,n):
    x = list(set(list(s[:i])))
    y = list(set(list(s[i:])))
    c, d = 0, 0
    for j in x:
        c += y.count(j)
    for j in y:
        d += x.count(j)
    ans = max(c,d,ans)
print(ans)

ABC064-C
2WA

考えたこと
条件を書き間違えて2WA出ました。3200以上の人が3200以下の人の色に合せるか、3200以下の人がいなければ3200以上の人が同じ色にしたときが最小値です。3200以上の人がそれぞれ別の色にしたときが最大値を取ります。

n = int(input())
a = list(map(int,input().split()))

check = [False] * 8
r = 0
for i in range(n):
    if a[i] < 400:
        check[0] = True
    elif 400 <= a[i] < 800:
        check[1] = True
    elif 800 <= a[i] < 1200:
        check[2] = True
    elif 1200 <= a[i] < 1600:
        check[3] = True
    elif 1600 <= a[i] < 2000:
        check[4] = True
    elif 2000 <= a[i] < 2400:
        check[5] = True
    elif 2400 <= a[i] < 2800:
        check[6] = True
    elif 2800 <= a[i] < 3200:
        check[7] = True
    else:
        r += 1

color = 0
ans_max = r
for i in range(8):
    if check[i]:
        color += 1
        ans_max += 1

ans_min = max(color,1)
print(ans_min,ans_max)

ABC060-B
1WA,1TLE

考えたこと
いくつAの倍数を足してもAの倍数なので、aを全探索しました。範囲はbまででいいのですが、10**5くらいまで探索しました。

import math
a, b, c = map(int,input().split())

for i in range(10**5):
    n = a * i
    if n % b == c:
        print('YES')
        quit()
print('NO')

ABC072-C

考えたこと
それぞれの操作で生れる整数を記録して、それらの最大値を取ります。操作で同じ数は生れない(整数に+1、-1、+0するから)のでこの方法で解けました。

n = int(input())
a = list(map(int,input().split()))

num = [0] * (10**5+3)
for i in range(n):
    num[a[i]] += 1
    num[a[i]+2] += 1
    num[a[i]+1] += 1

print(max(num))

ABC057-B

考えたこと
それぞれの生徒に対して全てのチェックポイントとの距離を計算して、最小値を取っています。

n, m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
cd = [list(map(int,input().split())) for _ in range(m)]

for i in range(n):
    dis = []
    for j in range(m):
        dis.append(abs(ab[i][0]-cd[j][0])+abs(ab[i][1]-cd[j][1]))
    ans = dis.index(min(dis))
    print(ans+1)

ABC107-B

考えたこと
列と行の処理がすごーく面倒でした。汚いコードですが...

h, w = map(int,input().split())
a = [input() for _ in range(h)]

remove_a = []
for i in range(h): #横の'.'の繋りを削除
    if '#' in a[i]:
        remove_a.append(list(a[i]))

n = len(remove_a)
l = []
for i in range(w):
    s = ''
    for j in range(n):
        s += remove_a[j][i]
    if '#' in s: #縦の'.'の繋りを削除
        l.append(list(s))

ans = ''
for i in range(len(l[0])): #上の操作によって縦と横が入れ替わっているので、直す
    s = ''
    for j in range(len(l)):
        s += l[j][i]
    ans += s
    if i != len(l[0])-1: #最後の行は改行しない
        ans += '\n' #改行を忘れずに
print(ans)

まとめ

最後の問題以外は簡単だった。ではまた、おやすみなさい

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