본문 바로가기
알고리즘/백준

[백준] 2566 - 최댓값 (JAVA/ 자바)

by pandastic 2024. 3. 17.
반응형

2566번 문제(1)
2566번 문제(2)

 

1. Scanner를 이용한 방법

import java.util.Scanner;

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

    int[][] A = new int[9][9];
    int max = -1;
    int x = 0;
    int y = 0;
    
    for(int i=0; i<9; i++){
        for(int j=0; j<9; j++){
            A[i][j] = sc.nextInt();
        }
    }
    for(int i=0; i<9; i++){
        for(int j=0; j<9; j++){
          if(A[i][j] > max){
            max = A[i][j];
            x = i+1;
            y = j+1;
          } 
        }
    }
    System.out.println(max);
    System.out.println(x + " " + y);
    
    sc.close();
    }
}

 

2. BufferedReader를 이용한 방법

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

 

0부터 시작하고 있으므로 i와 j에 1을 더해준다.

반응형