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?

【Python】ABC012-A解説

0
Last updated at Posted at 2026-02-22

問題概要

2つの整数 A, Bが与えられる。順番を入れ替えて出力する。

解法と実装

与えられた入力を逆にして出力すれば大丈夫です。
Pythonではカンマ区切りでprintすると、デフォルトで間に半角スペースが入ります。

A, B =map(int, input().split()) # 入力を受け取る
print(B, A) # 逆にして出力

スワップを使って入れ替えることもできます。

A, B = map(int, input().split())
A, B = B, A #  AとBの入れ替え
print(A, B)

代入によって入れ替える時は、一時変数を利用します。
ここでは、A = Bとした時に、 AもBも元々Bに受け取った値になってしまうので、Bに代入するための値を持つために一時変数を使います。

A, B = map(int, input().split())
tmp = A # 一時変数を用意
A = B # AにBを代入
B = tmp # Bに一時変数を代入
print(A, B)
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?