📝 문제
🔑 풀이 과정
나 같은 경우는 배열을 사용해서 배열에 입력받은 후, 반복문으로 배열의 최댓값을 찾는 식으로 진행.
배열을 굳이 선언하지 않고도 입력 받으면서 바로바로 최댓값과 비교하여 최댓값과 인덱스를 저장하는 방법도 있다.
🔓 답안
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int[] arr = new int[6]; //인덱스 5번까지 쓰기 위함
// 입력
for(int i = 1; i < 6; i++){
int sum = 0;
st = new StringTokenizer(br.readLine());
for(int j = 0; j < 4; j++){
sum += Integer.parseInt(st.nextToken());
}
arr[i] = sum;
}
//최댓값 찾기
int max = 0;
int max_index = 0;
for(int i = 1; i < 6; i++){
if(arr[i] > max){
max = arr[i];
max_index = i;
}
}
bw.write(max_index + " " + max + "\n");
bw.flush();
bw.close();
}
}
🖤 알고리즘 분류
- 수학
- 구현
- 사칙연산
'PS > Baekjoon' 카테고리의 다른 글
[Baekjoon] 2606 - 바이러스 (0) | 2023.02.12 |
---|---|
[Baekjoon] 15963 - CASIO (0) | 2023.02.11 |
[Baekjoon] 17618 - 신기한 수 (0) | 2023.02.09 |
[Baekjoon] 1436 - 영화감독 숌 (0) | 2023.02.08 |
[Baekjoon] 1018 - 체스판 다시 칠하기 (0) | 2023.02.07 |
댓글