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?

More than 1 year has passed since last update.

Paiza Cランク獲得問題(Dランク相当) 配列メニュー C#

Last updated at Posted at 2023-08-02

paizaのCランク獲得問題の解答がなかったので、備忘録として残します。
明らかに簡単な問題の場合は省略しますが、個人的に少しでも考えたコードを残していきたいと思います。
初心者ですので醜いコードを書きますが、温かい目で見守っていただけると嬉しいです。
また、より良い記述方法などありましたら、コメント等で教えていただけると嬉しいです。

目次

  • 【次元配列の入出力】i番目の出力 Boss
  • 要素数の出力
  • 全要素の出力
  • 行数の出力
  • 各行の要素数の出力
  • 二次元配列の入力 2
  • 二次元配列の入力 4
  • i番目の出力 1
  • 【二次元配列の入出力】i番目の出力 Boss

【次元配列の入出力】i番目の出力 Boss

問題文

using System;
using System.Linq;
class Program
{
    static void Main()
    {

        int[] array1 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int[] array2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        Console.WriteLine(array2[array1[1]-1]);

    }
}

要素数の出力

問題文

using System;
class Program

{
    static void Main()
    {
        int [,] array=new int[2,6]{
            { 1, 2, 3, 4, 5, 6 },
            { 8, 1, 3, 3, 1, 8 }
        };
        int column=array.GetLength(0);
        int row=array.GetLength(1);
        Console.WriteLine(column*row);
    }
}

全要素の出力

問題文

using System;
class Program

{
    static void Main()
    {
        int [,] array=new int[2,6]{
            { 6, 5, 4, 3, 2, 1 },
            { 3, 1, 8, 8, 1, 3 }
        };
        for(int i=0;i<2;i++){
            for(int j=0;j<6;j++){
                if(j<5){
                    Console.Write(array[i,j]+" ");
                }else{
                    Console.WriteLine(array[i,j]);
                }
            }
        }
    }
}

行数の出力

問題文

using System;
class Program

{
    static void Main()
    {
        int [,] array=new int[3,3]{
            { 1, 2, 3},
            { 4, 5, 6},
            { 7, 8, 9}
        };
        int column=array.GetLength(0);
        Console.WriteLine(column);
    }
}

列数の出力も一緒です。

各行の要素数の出力

問題文

using System;
class Program

{
    static void Main()
    {
        int [][] array=new int[3][];
        array[0]=new int[] {1};
        array[1]=new int[] {2 ,3};
        array[2]=new int[] {4 ,5, 6};
        for(int i=0;i<array.Length;i++){
            Console.WriteLine(array[i].Length);
        }
    }
}

ジャグ配列(配列の配列)を用いました。

二次元配列の入力 2

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        int M=int.Parse(Console.ReadLine());
        int [,]array=new int[5,M];
        for(int i=0;i<5;i++){
            int [] row=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            for(int j=0;j<M;j++){
                array[i,j]=row[j];
            }
        }
        for(int i=0;i<5;i++){
            for(int j=0;j<M;j++){
                if(j<M-1){
                    Console.Write(array[i,j]+" ");
                }else{
                    Console.WriteLine(array[i,j]);
                }
            }
        }
    }
}

ジャグ配列使う方が楽です多分
次の二次元配列の入力3もほぼ同じです

二次元配列の入力 4

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        int [] NM=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int N=NM[0];
        int M=NM[1];

        int [,]array=new int[N,M];
        for(int i=0;i<N;i++){
            int [] row=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            for(int j=0;j<M;j++){
                array[i,j]=row[j];
            }
        }
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                if(j<M-1){
                    Console.Write(array[i,j]+" ");
                }else{
                    Console.WriteLine(array[i,j]);
                }
            }
        }
    }
}

二次元配列の入力2とほぼ同じです

i番目の出力 1

問題文

using System;
class Program

{
    static void Main()
    {
        int [,] array=new int[4,4]{
            { 1, 2, 3, 4},
            { 10, 100, 0, 5},
            { 8, 1, 3, 8},
            { 15, 34, 94, 25}
        };
        string [] KL=Console.ReadLine().Split();
        int K=int.Parse(KL[0]);
        int L=int.Parse(KL[1]);
        Console.WriteLine(array[K-1,L-1]);
    }
}

【二次元配列の入出力】i番目の出力 Boss

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        int [] NMKL=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int N=NMKL[0];
        int M=NMKL[1];
        int K=NMKL[2];
        int L=NMKL[3];

        int [,]array=new int[N,M];
        for(int i=0;i<N;i++){
            int [] row=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            for(int j=0;j<M;j++){
                array[i,j]=row[j];
            }
        }
        
        Console.WriteLine(array[K-1,L-1]);
    }
}

基本同じです。

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?