0
0

3 * 3 の出力

Last updated at Posted at 2023-11-14

前回につづいてこちらも。
https://paiza.jp/works/mondai/stdout_primer/stdout_primer__2dim_array_step2

入力された半スペで区切られている 9 個の数値を 3 行 3 列の形式で出力という形。
前と同じ解法でいけるのでこちらで。

tmp = input()
N = tmp.split()
for i in range(9):
    if (i + 1) % 3 != 0:
        print(N[i],end=" ")
    else:
        print(N[i],end="\n")

・Nにいれるところは
 N = input().split() とやると1行でできる

・他の方法として数値は普通にprintして、他の解法として半スペか改行をi番目ごとにprintするというのがある。下のようになる
 (print()はそのままだと改行するから)

for i in range(9):
    print(N[i])
    if i % 3 == 2:
        print()
    else:
        print(" ",end="")
0
0
1

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