글 작성자: 취업중인 피터팬
728x90

https://www.acmicpc.net/problem/2566

 

2566번: 최댓값

첫째 줄에 최댓값을 출력하고, 둘째 줄에 최댓값이 위치한 행 번호와 열 번호를 빈칸을 사이에 두고 차례로 출력한다. 최댓값이 두 개 이상인 경우 그 중 한 곳의 위치를 출력한다.

www.acmicpc.net

 

문제 설명

행열에 대해 최대값과 그 위치를 찾아 출력하는 문제입니다.

 

문제 풀이

2차원 배열을 선언하고 이중 for문을 통해 최대값을 구했습니다. 코드를 보시면 바로 이해가 되실 겁니다.

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int max = 0;
        int row = 0;
        int colum = 0;

        int[][] arrayRe = new int[9][9];

        for(int i=0; i<arrayRe.length; i++){
            for(int j=0; j<arrayRe[i].length; j++){
                arrayRe[i][j] = sc.nextInt();
            }
        }
/*
        int[][] arrayRe = {{3, 23, 85, 34, 17, 74, 25, 52, 65},
                {10, 7, 39, 42, 88, 52, 14, 72, 63},
                {87, 42, 18, 78, 53, 45, 18, 84, 53},
                {34, 28, 64, 85, 12, 16, 75, 36, 55},
                {21, 77, 45, 35, 28, 75, 90, 76, 1},
                {25, 87, 65, 15, 28, 11, 37, 28, 74},
                {65, 27, 75, 41, 7, 89, 78, 64, 39},
                {47, 47, 70, 45, 23, 65, 3, 41, 44},
                {87, 13, 82, 38, 31, 12, 29, 29, 80}};

 */

        for(int i=0; i<arrayRe.length; i++){
            for(int j=0; j<arrayRe[i].length; j++){
                if(max <= arrayRe[i][j]){
                    max = arrayRe[i][j];
                    row = i;
                    colum = j;
                }
            }
        }

        System.out.println(max);
        System.out.println(row+1);
        System.out.println(colum+1);
    }
}