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?

問題概要

偶数文字の文字列Sが与えられる。
この文字数を2Nとする時、各0<i≦Nに対して、i番目とi+1番目の文字を入れ替えて出力する。

解法と実装

for文で順番に文字を確認し、偶数番目の時はi+1番目の文字、奇数番目の時はi-1番目の文字を出力します。
print関数にend=""という引数をつけることで、改行が行われません。

S = input() # 入力を受け取る
for i in range(len(S)): # for文で順番に確認
  if i % 2 == 0: # 偶数番目の時
    print(S[i+1], end="") # i+1番目を出力
  else:
    print(S[i-1], end="")

i番目とi+1番目の文字を入れ替えるので、S[2i]とS[2i+1]だったものを入れ替えて出力することで正解できます。

S = input() # 入力を受け取る
for i in range(len(S)//2): # Sの文字数の半分までfor文を回す
  print(S[2*i+1] + S[2*i], end="") # 順番を入れ替えて出力
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?