LoginSignup
0
0

More than 3 years have passed since last update.

はじめに

前回
4日目です。

#4

問題
わーい。B問題だー

考えたこと
入力される[町の名前、人口]を人口でsortして、一番大きい町が過半数以上あるか判別します。
私の場合は、[町の名前、人口]みたいに二次元配列なので普通にsortできないので、

s.sort(key=lambda x: int(x[1]),reverse=True)

でsortしました。参考にしたページ
おしゃれでかっこいいので好き。x: int(x[n])で要素のindexを指定してsortできます。今回は人口でsortしたいのでx[1]、大きい順にしたいのでreverse=True。

人口はfor文で計算しました。

population = 0
for i in range(n):
    population += int(s[i][1])

ちゃんと変数名を分かりやすく書きました。少し長いけど。
あとはif文に入れるだけ。

n = int(input())
s = [list(input().split()) for _ in range(n)]

population = 0
for i in range(n):
    population += int(s[i][1])

s.sort(key=lambda x: int(x[1]),reverse=True)

if int(s[0][1]) > population//2:
    print(s[0][0])
else:
    print('atcoder')

最後に、print('atcoder')のスペルミスに気をつければ大丈夫。

まとめ

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