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?

問題概要

高橋君はドリンクを飲もうとしている。
定価P円または、D品の食事のいずれかとセットでの割引Q円のどちらの方が安くドリンクを飲めるか。

解法と実装

min関数を使うことで、複数の値のうち小さいものを出力することができます。
今回の問題では、食べ物はN個あるうち一番安いものについて考えれば大丈夫なので、for文で比較して小さいものを選んでいます。
foodの初期値は、制約にあるDの上限10^5よりも大きい値を入れています(視覚的に分かりやすいよう、100100100のような数字を入れることが多いです)。

N, P, Q = map(int, input().split())
D = list(map(int, input().split())) # 食べ物の値段をlistで受け取る
food = 100100 # foodの初期値は制約より大きい
for i in range(N):
  food = min(food, D[i]) # foodとDのi番目のうち小さいものをfoodに代入
print(min(P, food + Q)) # P円と食べ物+Q円のうち、安い方を出力

Dをまとめてlistで受け取り、min関数を使うと簡単に実装ができます。

N, P, Q = map(int, input().split())
D = list(map(int, input().split()))
food = min(D) # list内の最小値を受け取る
print(min(P, food + Q))
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?