[백준] C# : 행렬 덧셈 (2738번)

ssu_hyun·2022년 7월 15일
0

Data Structure & Algorithm

목록 보기
29/67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            // 행렬 크기 입력받기
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);

            // n*m 크기의 행렬 A,B 만들기
            int[,] A = new int[n, m];
            int[,] B = new int[n, m];

            // 행렬 A에 값 삽입
            for (int i = 0; i < n; i++)
            {
                // 한 줄 입력 받고
                string[] line = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    // 한 줄의 각 요소를 int형변환 후 행렬에 저장하기
                    A[i, j] = int.Parse(line[j]);
                }
                
            }

            // 행렬 B에 값 삽입
            for (int i = 0; i < n; i++)
            {
                // 한 줄 입력 받고
                string[] line = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    // 한 줄의 각 요소를 행렬에 저장하기
                    B[i, j] = int.Parse(line[j]);
                }
            }

            // 행렬 덧셈 및 출력
            for (int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    Console.Write(A[i, j] + B[i, j] + " ");  
                }
                Console.WriteLine();  // 다음 줄로 
            }
        }
    }
}
/*  배열  */


// 초기화
string[] s = new int[3]{"안녕", "Hello", "Halo"};  // 기본
string[] s = new int[]{"안녕", "Hello", "Halo"};  // 요소 개수 생략
string[] s = {"안녕", "Hello", "Halo"};  // new 연산자, 형식, 요소 개수 생략


// 접근
int[] scores = new int[5]
scores[^1] = 34;  // 마지막 인덱스 요소


// 분할
System.Range r1 = 0..3;  // 시작 인덱스..마지막 인덱스(제외)
int[] sliced = scores[r1]  // 분할된 배열 반환


// 2차원 배열
int[,] array = new int[2,3];  // 선언
Console.WriteLine( array[ 0, 2 ] );  // 접근
int[,] arr = new int[2, 3] {{1, 2, 3}, {4, 5, 6}};  // 초기화1
int[,] arr = new int[,] {{1, 2, 3}, {4, 5, 6}};  // 초기화2
int[,] arr = {{1, 2, 3}, {4, 5, 6}};  // 초기화3


// 다차원 배열
int[, ,] array = new int[4, 3, 2] 
            { 
                { {1, 2}, {3, 4}, {5, 6} },
                { {1, 4}, {2, 5}, {3, 6} },
                { {6, 5}, {4, 3}, {2, 1} },
                { {6, 3}, {5, 2}, {4, 1} },
            };

            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    Console.Write("{ ");
                    for (int k = 0; k < array.GetLength(2); k++)
                    {
                        Console.Write($"{array[i, j, k]} ");
                    }
                    Console.Write("} ");
                }
                Console.WriteLine();
            }


// 가변 배열 : 가변 배열의 요소로 입력되는 배열은 그 길이가 모두 같을 필요가 없다
int[][] jagged = new int[3][];
jagged[0] = new int[5] { 1, 2, 3, 4, 5 };  // 5
jagged[1] = new int[] { 10, 20, 30 };  // 3
jagged[2] = new int[] { 100, 200 };  // 2
// 선언 + 초기화
int[][] jagged2 = new int[2][] { 
       new int[] { 1000, 2000 }, 
       new int[4] { 6, 7, 8, 9 } };

1개의 댓글

comment-user-thumbnail
2023년 3월 27일

감사합니다

답글 달기