출처 : 반크 카드뉴스
문제]
행렬의 행과 열을 바꾸는 메소드인 transpose를 작성하고, 다음 데이터를 이용하여 테스크하시오.
1 2 3
4 5 6
참고풀이]
using System;
using System.Collections.Generic;
using System.Text;
namespace Chapter3
{
class P151_3_9_11
{
public static int[,] transpose(int[,] P1)
{
int i,j;//P1의 행열변수
int[,] P2 = new int[P1.GetLength(1),P1.GetLength(0)];
//행열을 맞바꾸기
for (i = 0; i < P1.GetLength(0); i++)
for (j = 0; j < P1.GetLength(1); j++)
P2[j, i] = P1[i, j];
return P2;
}
public static void Main(string[] args)
{
int[,] A = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] B = new int[A.GetLength(1), A.GetLength(0)];
int r, c;//행열변수
Console.WriteLine("배열의 행열을 변환하기 전 값]");
for (r=0;r<2;r++)
{
for (c = 0; c < 3; c++)
Console.Write("{0,2}", A[r,c]);
Console.WriteLine();
}
B=transpose(A);
//결과출력
Console.WriteLine("\n배열의 행열을 빠꾼 후 결과값]");
for (r = 0; r < B.GetLength(0); r++)
{
for (c = 0; c < B.GetLength(1); c++)
Console.Write("{0,2}", B[r, c]);
Console.WriteLine();
}
}
}
}
참고풀이 결과]
대한민국의 아름다운 영토, 독도의 여름
댓글