LoginSignup
0
0

More than 3 years have passed since last update.

【Python】ABC - 099 - Stone Monument

Last updated at Posted at 2019-09-23

問題

$ 1, (1+2), (1+2+3), \ldots, (1+2+3 \ldots +999)$と999本立っている鉄塔のうち、隣り合う2塔(西側と東側)の雪に埋まって見えている部分の高さが$a, b$として与えられたときに、雪が何メートル積もっているかを求める問題。

方針

  1. $b-a$が隣り合う鉄塔の高さの差分$h$を表現しており、これにより西側の鉄塔の高さが$1+2+3+ \ldots + h-1$であるとわかる
  2. $1+2+3 \ldots + h-1$を求めて、$a$を引くと雪の積もっている高さがわかる
a, b = map(int, input().split())
diff_ = b - a
pos1 = sum([i for i in range(1, diff_)])
print(pos1 - a)

追記

@konandoiruasa にコメントいただいたので、追記。

方針2.で$1+2+3+ \ldots + h-1$を求めるのに、sum()を用いて求めていたが、
初項$1$、末項$h-1$、項数$h-1$の等差数列の和の公式で求めることができる。

pos1 = h*(h-1) / 2
0
0
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
0
0