LoginSignup
0
1

More than 3 years have passed since last update.

はじめに

前回
Boot camp for BeginnersのMediumを3問解きます。

#53

ABC118-C

考えたこと
やってることが互除法に似ているなと思ってサンプルケースを見ると、勘が合っていたのでそのまま実装しました。全ての要素についての最大公約数を計算します。

import fractions #mathじゃない
n = int(input())
a = list(map(int,input().split()))

ans = fractions.gcd(a[0],a[1])
for i in range(2,n):
    ans = fractions.gcd(ans,a[i])
print(ans)

ABC067-C

考えたこと
それぞれの場合を全て計算する。毎回sumするのはもったいないので、うまく計算する

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

d = sum(a)
ans = float('inf')
for i in range(n-1):
    if i == 0:
        x = a[0]
        y = d - x
    else:
        x += a[i]
        y -= a[i]
    ans = min(abs(x-y),ans)
print(ans)

ABC070-C

考えたこと
典型的な最小公倍数の問題

import fractions

n = int(input())
t = [int(input()) for _ in range(n)]

ans = t[0]
for i in range(1,n):
    ans = ans * t[i] // fractions.gcd(ans, t[i])

print(ans)

まとめ

生活リズムが壊れているせいで、問題に集中できなってるので直します。ではまた。

0
1
2

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