LoginSignup
0
1

More than 3 years have passed since last update.

はじめに

前回
今日のよるかつの問題を解きます。

A問題

問題

考えたこと
setに入れてlenするだけ

n = int(input())
s = set(input().split())

if len(s) == 4:
    print('Four')
else:
    print('Three')

B問題

問題

考えたこと
ちょっと前にやりました。全てのマスに隣接している全てのマスを調べる

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

g = [[]*w for _ in range(h)]

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

ans = ''
for i in range(h): #このへんの書き方が汚ない
    c = ''.join(g[i])
    ans += c
    if i != h-1:
        ans += '\n'

print(ans)

C問題

問題

考えたこと
前にコメントで二乗の総和は各要素の平均値を取ったときに最小になることを教えてもらったので、実装。平均はstatisticsを使った。lenとsumを使っても実装できます。PyPyにstatisticsは無いので注意

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

mean = round(statistics.mean(a))

ans = 0
for i in range(n):
    ans += (a[i]-mean)**2

print(ans)

D問題

問題

考えたこと
最初の一往復目は普通の最小距離。二往復目は一周外を周ればいい。

sx, sy, tx, ty = map(int,input().split())

n = tx - sx #x座標の距離
m = ty - sy #y座標の距離

ans = 'R' * n + 'U' * m + 'L' * n + 'D' * m + 'L' + 'U' * (m+1) + 'R' * (n+1) + 'D' + 'R' + 'D' * (m+1) + 'L' * (n+1) + 'U'
print(ans)

まとめ

Eは問題の意味が理解できなかったのでリタイア。40分で4完でした。簡単な問題だったので集中して解けばもっと早く解けた気がする。ではまた、おやすみなさい

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