
🔍 접근방법
- 배열에 입력값을 저장한 후 for문으로 값을 비교해가며 최대값 최소값을 구한다.
- 배열에 입력값을 저장한 후 array.sort()를 이용해 정렬한 후 최대값 최소값을 구한다.
- 배열에 입력값을 저장한 후 Stream으로 최대,최소값을 구한다.
💡 코드
public class B10818 {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
String input = br.readLine();
StringTokenizer st = new StringTokenizer(input," ");
int arr[] = new int[N];
for(int i=0; i<N; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
int min = arr[0];
int max = arr[0];
for(int x=0; x < arr.length; x++){
if(arr[x] < min){
min = arr[x];
}
}
for(int y=0; y < arr.length; y++){
if(arr[y] > max){
max = arr[y];
}
}
bw.write(min + " " + max);
bw.close();
Arrays.sort(arr);
bw.write(arr[0] + " " + arr[arr.length-1]);
bw.close();
bw.write("최대값 : " + Arrays.stream(arr).max().getAsInt());
bw.write("최소값 : " + Arrays.stream(arr).min().getAsInt());
bw.close();
}
}