✏️ 문제풀이/백준

[백준/Java] 2566번 :: 최댓값

bono-hye 2024. 4. 22. 22:42

 

🌱 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main
{
	public static void main(String[] args) throws IOException 
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int max = 0;
		int x = 1;
		int y = 1;
		
		for(int i=0; i<9; i++)
		{
			StringTokenizer st = new StringTokenizer(br.readLine());
			
			for(int j=0; j<9; j++)
			{
				int num = Integer.parseInt(st.nextToken());
				if(num > max)
				{
					max = num;
					x = i+1;
					y = j+1;		
				}
			}
		}
		
		System.out.println(max);
		System.out.println(x + " " + y);
		
	}
}

 

💡 정리

예제로 테스트 했을 때는 오류가 안났는데 제출하니까 90%후반대에서 계속 오류가 났다.

그때의 코드는 x와 y를 0으로 초기화했다.

뭐가 틀린지 모르겠어서 질문 게시판을 보니 81개의 숫자가 모두 0이면 두번째 for문의 if문을 수행하지 못해서 x, y가 0으로 출력된다는 것을 알게되었다!!! 반례를 찾는 것은 참 중요하다!!!

x, y를 1로 초기화 해주는 코드로 바꾸니 해결 완료~!