본문 바로가기
프로그래밍 문제/c# 문제

60. c# 2차원 배열 테두리 합 구하기

by 지나팩 2025. 1. 24.

주어진 2차원 배열의 외곽 테두리의 합을 구하여 출력하는 코드를 작성하세요.

 

출력 예 : 2차원 배열 테두리 요소의 합 : 40

 

 

코드는 아래에~~~~~~~

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
 
class Program
{
    static void Main()
    {
        int[,] array = {
            {123},
            {456},
            {789}
        };
 
        int sum = 0;
 
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                if (i == 0 || i == array.GetLength(0- 1 || j == 0 || j == array.GetLength(1- 1)
                {
                    sum += array[i, j];
                }
            }
        }
 
        Console.WriteLine("테두리 합: " + sum);
    }
}
 
cs

댓글