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?

問題概要

A op B という形式の入力が与えられる。ここで、A, Bは整数であり、opは+または-である。
計算結果を出力せよ。

解法と実装

2文字目が+なのか-なのかで分けて答えを出力します。
+や-をint型として受け取ることはできないので、答えを出力する時に数字の部分をint型にして計算しています。

a, b, c = input().split() # 入力を受け取る
if b == "+": # +の時
  print(int(a) + int(c)) # int型にして計算
else:
  print(int(a) - int(c))

リストとして入力を受け取ることもできます。

A = list(input().split()) # listとして入力を受け取る
if A[1] == "+":
  print(int(A[0]) + int(A[2]))
else:
  print(int(A[0]) - int(A[2]))
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?