0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

問題概要

3つの入力が与えられる。その3つの数字の中央値を出力せよ。

解法と実装

3つの値の中央値は、3つの値のうち最大でも最小でもないものになります。
そのため、順番に並び替えて、真ん中にくるものを出力することで答えが得られます。

A = list(map(int, input().split())) # リストとして入力を受け取る
A.sort() # Aをソートする
print(A[1]) # 0-indexでの1番目の値を出力する

それぞれの場合をif文で場合分けすることもできます。
それぞれの入力が同じ値のこともあるので、等号付き不等号を使います。

a, b, c = map(int, input().split()) # それぞれの値をa, b, cに受け取る
if a >= b and b >= c: # それぞれのパターンを比較
  print(b)
elif a >= c and c >= b:
  print(c)
elif b >= a and a >= c:
  print(a)
elif b >= c and c >= a:
  print(c)
elif c >= a and a >= b:
  print(a)
else:
  print(b)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?