본문 바로가기

Algorithm/Baekjoon

[백준_JAVA] 2566 최댓값

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String[][] arrList = new String[9][9];
		String[] arr;
		String str = null;
		int max = 0; int row = 0; int col = 0;
		
		for(int i = 0; i < arrList.length; i++) {
			str = scan.nextLine();
			arr = str.split(" ");
			for(int j = 0; j < arr.length; j++) {
				arrList[i][j] = arr[j];
				
				int currentNum = Integer.parseInt(arr[j]);
				if(currentNum > max) {
					max = currentNum;
					row = i+1; col = j+1;
				}
			}
		}
		System.out.println(max+"\n"+row+" "+col);
		scan.close();
	}
}

위의 코드에서 에러가 발생했다.

 

scanner가 아닌 BufferReader을 사용하여 해결한다.

 

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[][] arrList = new int[9][9];
		int max = 0; int row = 0; int col = 0;
		
		for(int i = 0; i < arrList.length; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			for(int j = 0; j < arrList[i].length; j++) {
				arrList[i][j] = Integer.parseInt(st.nextToken());
				
				if(arrList[i][j] >= max) {
					max = arrList[i][j];
					row = i+1; col = j+1;
				}
			}
		}
		System.out.println(max);
		System.out.println(row+" "+col);
	}
}

max가 0일수도 있었다...

if문의 범위를 수정한다