N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.
첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.
첫째 줄에 주어진 정수 N개의 최솟값과 최댓값을 공백으로 구분해 출력한다.
import java.io.*;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String [] arr = br.readLine().split(" ");
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i< n; i++){
int num = Integer.parseInt(arr[i]);
if(num<min){
min = num;
}
if(num > max){
max = num;
}
}
System.out.println(min + " " + max);
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
백준 - 10813.공 바꾸기 (0) | 2025.01.17 |
---|---|
백준 -10807.개수 세기 (0) | 2025.01.17 |
백준 - 10871. X보다 작은 수 (1) | 2025.01.17 |
백준 -10951. A+B -5 (0) | 2025.01.17 |
백준 - 10952.A+B - 5 (0) | 2025.01.17 |