0
0

AtCoder ABC322-A

Last updated at Posted at 2023-11-11

AtCodeのビギナー用A問題をひたすら解いてます。
簡単なのでモチベーションあがります。
正解だったよっていうのを載せます!
たまーにB問題を解いて苦しんでます。。。💦

AtCoder Beginner Contest 322

image.png

n=int(input())
s=input()

ans=s.find('ABC')+1 #文字列を検索し、ABCの位置を取得。indexを取得するので+1しておく。
if ans==0:      #ABCが見つからなかったら
    print(-1)
else:           #ABCが見つかったら
    print(ans)

if文、ずっとこうやって書いてきたけど、1行で書く方法があったらしい!
7行あったコードが4行になった。。。

n=int(input())
s=input()

ans=s.find('ABC')+1 #文字列を検索し、ABCの位置を取得。indexを取得するので+1しておく。
print(-1) if ans==0 else print(ans) #←1行にした

要約するとこんな感じ
Trueの処理 if 条件式 else Falseの処理

今日も1つ賢くなりました:v:

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