[JAVA/2562번] 최댓값

고지훈·2021년 9월 6일
1

Algorithm

목록 보기
13/68
post-thumbnail

문제


입력 및 출력


풀이

import java.io.*;

class Main {
    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int max = 0, count = 0;
        int[] numArray = new int[9];

        for (int i = 0; i < numArray.length; i++) {
            numArray[i] = Integer.parseInt(br.readLine());
            if (max < numArray[i]) {
                max = numArray[i];
                count = i + 1;
            }
        }
        System.out.println(max);
        System.out.println(count);
    }
}

결과 및 해결방법

[결과]

[정리]

해결방법

  • 이번 문제는 입력한 숫자 중 최댓값을 찾는 문제로, max 변수의 초깃값을 0으로 설정한 후 입력받은 숫자 하나씩 비교하여 max보다 크면 max의 값에 해당 숫자를 저장하였다.
  • 마찬가지로 count 값 또한 max의 값이 변경될 때 마다 인덱스에 1을 더한 값을 저장하였다. 이유는 배열의 인덱스는 0부터 시작하기 때문이다.
profile
"계획에 따르기보다 변화에 대응하기를"

0개의 댓글