문제
https://www.acmicpc.net/problem/10818
입력된 숫자들 중에서 최솟값과 최댓값을 찾는 문제였다.
문제 풀이
1. Scanner
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] A = new int[N];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
if (A[i] > max) {
max = A[i];
}
if (A[i] < min) {
min = A[i];
}
}
System.out.printf("%d %d",min,max);
}
}
2. BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
String[] input = reader.readLine().split(" ");
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
int num = Integer.parseInt(input[i]);
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.printf("%d %d", min, max);
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준/JAVA]3052번 나머지 (0) | 2025.01.10 |
---|---|
[백준/JAVA]5597번 과제 안 내신 분..? (0) | 2025.01.10 |
[백준/JAVA]10871번 X보다 작은 수 (0) | 2025.01.09 |
[백준/JAVA]10807번 개수 세기 (0) | 2025.01.09 |
[백준/JAVA]2884번 알람 시계 (0) | 2025.01.09 |