프로그래밍 문제/c# 문제
59. c# 2차원 배열 행렬 전치 문제
지나팩
2025. 1. 21. 13:55
주어진 2차원 배열의 행과 열을 바꾼 전치 행렬을 출력하세요.
출력 예:
코드는 아래에~~~~
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
30
31
32
33
34
35
|
using System;
class Program
{
static void Main()
{
int[,] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = array.GetLength(0);
int cols = array.GetLength(1);
int[,] transposed = new int[cols, rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
transposed[j, i] = array[i, j];
}
}
for (int i = 0; i < transposed.GetLength(0); i++)
{
for (int j = 0; j < transposed.GetLength(1); j++)
{
Console.Write(transposed[i, j] + " ");
}
Console.WriteLine();
}
}
}
|
cs |