1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

問題概要

マウスのクリック時間を表すN個の入力が与えられる。2回の連続した入力時間の差がD以下の時、ダブルクリックに成功している。
ダブルクリックしたなら最初にダブルクリックした時間を、ダブルクリックできていないなら-1を出力する。

解法と実装

配列で時間を受け取り、順番に確認します。
i+1番目とi番目の比較なので、rangeはN-1になります。

N, D = map(int, input().split()) # 入力を受け取る
T = list(map(int, input().split())) # リストとしてクリック時間を受け取る
ans = -1 # 答えの初期値は-1

for i in range(N-1):
  if (T[i+1] - T[i] <= D): # i+1番目とi番目の時間の差がD以下の時
    ans = T[i+1]
    break
print(ans)

備考

Pythonでは入力の受け取りが1行ごとなので、リストを使うのが良いと思います。
C++などでは空白区切りで受け取るので、for文で毎回受け取りながらチェックするのも有効です。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?