1
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?

二次元配列の入力

Posted at

今日は配列メニューの2次元配列の練習問題を解いたのでメモ!


🔍 問題概要

問題:
1行目に列数Mが与えられ、次の5行M列の整数が入力される。

やること:
各行をスペース区切りでそのまま出力。


入力例:

4
1 2 3 4
0 0 0 0
8 1 3 8
1 10 100 99
15 68 48 15

出力例:

1 2 3 4
0 0 0 0
8 1 3 8
1 10 100 99
15 68 48 15



💣 NGコード例

const rl = require('readline').createInterface({input:process.stdin});
const lines = [];

rl.on('line',(input)=>{
    lines.push(input);
});


rl.on('close',()=>{
    const matrix = lines.slice(1).map(line => line.split(' '));
    matrix.forEach(a => console.log(a));
});

見た目はOKそう。でも出力はこうなる:

[ '1', '2', '3', '4' ]
[ '0', '0', '0', '0' ]
[ '8', '1', '3', '8' ]
[ '1', '10', '100', '99' ]
[ '15', '68', '48', '15' ]

このコードは、配列 をそのまま console.log() に渡している。
すると、配列を [要素, 要素, …] のように表示されてしまう。



✅ OKコード

const rl = require('readline').createInterface({input:process.stdin});
const lines = [];

rl.on('line',(input)=>{
    lines.push(input);
});

rl.on('close',()=>{
    const matrix = lines.slice(1).map(line => line.split(' '));
    
    const result = [];
    
    for(let i = 0; i < matrix.length; i++){
        const row = [];
        for(let j = 0; j < matrix[i].length; j++){
            row.push(matrix[i][j]);
        }
        result.push(row.join(' '));
    }
    
    result.forEach(row => console.log(row));
    
});
  • 2行目以降をスペースで分割して、2次元配列として取得。
  • 各行を1文字ずつループして新しい配列 row に再構築。
  • 最後に row.join(' ') でスペース区切りの文字列に変換して result に追加。
  • 配列としてではなく、スペースで連結した文字列を出力するので、期待通りの出力になる。


✅改善案

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', input => lines.push(input));

rl.on('close', () => {

    // 2行目以降を2次元配列として格納
    const matrix = lines.slice(1).map(line => line.split(' '));


    // ✅各行をスペース区切りで出力
    matrix.forEach(row => console.log(row.join(' ')));
});



📘メモ&まとめ

  • console.log([1, 2, 3])[1,2,3] になる(配列のまま表示)

  • console.log([1,2,3].join(' '))'1 2 3' になる(文字列として表示)

  • 実行結果が「一見正しそう」でも、形式が違うと評価NGになることもある!




僕の失敗談(´;ω;`)と解決法🐈

1
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
1
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?