0
0

[paiza]レベルアップ問題集解答案(C#)【N 行 M 列のデータの入力】行ごとに要素数の異なる整数列の入力

Last updated at Posted at 2024-09-25

意外と苦戦したので備忘録として…

問題

1 行目に整数 N が与えられます。
2 行目から (N + 1) 行目までの先頭に整数 M_i (1 ≦ i ≦ N) が与えられます。それに続いて M_i 個の整数 a_1, ..., a_{M_i} が与えられます。
上から i 番目、左から j 番目の整数は a_{i,j} です。
N 行の a_1, ..., a_M をそのまま出力してください。

入力例1
3
1 8
2 8 1
3 8 1 3

出力例1
8
8 1
8 1 3

C#解答案

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int num = int.Parse(Console.ReadLine());
        //二次元リストの宣言
        List<List<string>> list = new List<List<string>>();

        //二次元リストに要素追加
        for(int i=0; i<num; i++){
            string[] inputstr = Console.ReadLine().Split(' ');
            List<string> list2 = new List<string>(inputstr);
            list.Add(list2);
        }

        //二次元リストの各行の先頭を除き、スペース連結で出力
        List<string> li = new List<string>();
        for(int m=0; m<num; m++){
            Console.WriteLine(string.Join(" ",list[m].Skip(1)));
        }
    }
}
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