백준 온라인 코딩 문제풀이
Baekjoon Online Judge
Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.
www.acmicpc.net
코드 참고
wny0320 (박 상운)
Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
replit.com
2566번에 대해 다룸
2566
using System;
class Program
{
static void Main(string[] args)
{
int[,] input = new int[9, 9];
int maxValue = 0;
int[] pos = new int[2];
for(int col = 0; col < 9; col++)
{
int[] rowInput = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
for (int row = 0; row < 9; row++)
{
input[col, row] = rowInput[row];
}
}
for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 9; row++)
{
if (maxValue < input[col, row])
{
maxValue = input[col, row];
pos[0] = col+1;
pos[1] = row+1;
}
}
}
if(pos[0] == 0 && pos[1] == 0)
{
pos[0] = 1;
pos[1] = 1;
}
Console.WriteLine(maxValue);
Console.WriteLine(pos[0] + " " + pos[1]);
}
}
코드를 짜는 것은 지난번 행렬 덧셈을 하고 난 이후라 그렇게 어려운점은 없었음
다만 행렬의 위치에 관련해서 고민이 조금 필요했음
문제에서 주어진 행렬의 위치는 시작 지점이 1,1이지만 배열에서는 0,0
따라서 위치값을 저장할 때 배열 인덱스 값에 각각 +1을 해서 저장해주었음
또한 초기값이 0인 maxValue 같은 경우 0으로만 이루어진 배열을 받았을 때 자기 자신이 최대값임
따라서 pos의 값이 0,0으로 고정되는 현상이 발생함
이를 해결하기 위해 조건문을 달았음
- 조건문을 달지 않고 최대값 시작을 -1로 해도 됨
'BaekJoon[C#]' 카테고리의 다른 글
정렬 단계 2750 (0) | 2022.12.24 |
---|---|
2차원 배열 2563 (0) | 2022.12.23 |
2차원 배열 2738 (0) | 2022.12.21 |
기본 수학 2단계 4948 (0) | 2022.11.01 |
기본 수학 2단계 1929 (0) | 2022.10.31 |