https://www.acmicpc.net/problem/2562
2562번: 최댓값
9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어
www.acmicpc.net
방법1
intelliJ에서 잘 돌아가는데, 백준에서 틀림.
아시는 분은 댓글 달아주세요.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
List<Integer> newList = new ArrayList<>();
for(int i=0; i<9;i++){
newList.add(Integer.parseInt(br.readLine()));
}
int max = Collections.max(newList);
int idx = newList.indexOf(max);
System.out.println(max);
System.out.println(idx);
}
}
방법2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[9];
for(int i=0; i<9; i++){
arr[i] = Integer.parseInt(br.readLine());
}
int max = 0;
int idx = 0;
int cnt = 0;
for(int val : arr){
cnt++;
if(val > max){
max = val;
idx = cnt;
}
}
System.out.println(max);
System.out.println(idx);
}
}
'알고리즘 > [백준] JAVA' 카테고리의 다른 글
[백준] 3052 나머지 (0) | 2023.05.02 |
---|---|
[백준] 10813 공 바꾸기 (0) | 2023.04.28 |
[백준] 10818 최소, 최대 (0) | 2023.04.25 |
[백준] 10807 개수 세기 (0) | 2023.04.25 |
[백준] 25304 영수증 (0) | 2023.04.24 |