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

問題概要

4桁の暗証番号が与えられる。
4桁とも同じ数字かどうか判定せよ。

解法と実装

0始まりのこともあるので、string型で受け取ると判定しやすいです。

N = input() # 入力を受け取る
if N[0] == N[1] == N[2] == N[3]: # それぞれが同じか比較
  print("SAME")
else:
  print("DIFFERENT")

4桁で、それぞれの桁が同じものは10個しかないので、その中に同じものがあるかどうかでも判定できます。

N = input() # 入力を受け取る
A = ["0000", "1111", "2222", "3333", "4444", "5555", "6666", "7777", "8888", "9999"] # 同じ文字が4つ続くものをlistにする
if N in A: # Nと等しいものがAの中にある時
  print("SAME")
else:
  print("DIFFERENT")
1
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
1
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?