https://www.acmicpc.net/problem/10818
내 답안 - 1 (Scanner + 배열 사용)
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
int max = -1000001;
int min = 1000001;
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int[] arr = new int[N];
for (int i=0; i<arr.length; i++) {
int val = s.nextInt();
arr[i] = val;
if (min > arr[i]) min = arr[i];
if (max < arr[i]) max = arr[i];
}
System.out.println(min + " " + max);
}
}
내 답안 - 2 (BufferedReader + 배열 사용)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
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());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] arr = new int[N];
int max = -1000001;
int min = 1000001;
while(st.hasMoreTokens()) {
for (int i=0; i<arr.length; i++) {
arr[i] = Integer.parseInt(st.nextToken());
if (min > arr[i]) min = arr[i];
if (max < arr[i]) max = arr[i];
}
}
System.out.println(min + " " + max);
}
}
(+) 배열을 사용하지 않고도 풀어보기.
'Coding Test > 백준' 카테고리의 다른 글
[백준 2577번 - java] 숫자의 개수 (0) | 2021.11.08 |
---|---|
[백준 2562번 - java] 최댓값 (0) | 2021.11.08 |
[백준 1110번 - java] 더하기 사이클 (0) | 2021.11.04 |
[백준 10952번 - java] A+B - 5 (0) | 2021.11.04 |
[백준 10871번 - java] X보다 작은 수 (0) | 2021.11.04 |