반응형
https://www.acmicpc.net/problem/10818
문제 해결
이번 문제의 카테고리는 배열이었다. N개의 정수 중 최댓값과 최솟값을 출력하는 문제이다.
해결 방법
첫 번째 시도
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
StringTokenizer st;
int[] num = new int[count];
st = new StringTokenizer(br.readLine()," ");
for (int i = 0; i < num.length; i++) {
int A = Integer.parseInt(st.nextToken());
num[i] = A;
}
int max = num[0];
int min = num[0];
for(int i = 0 ; i < num.length; i++) {
for(int j = i + 1 ; j < num.length; j++) {
if(max < Math.max(num[i], num[j])) {
max = Math.max(num[i], num[j]);
}
if(min > Math.min(num[i], num[j])) {
min = Math.min(num[i], num[j]);
}
}
}
bw.write(min + " " + max);
br.close();
bw.flush();
bw.close();
}
}
나름 실행 시간 아껴보겠다고 Math클래스를 썼는데 결과는...
두 번째 시도
for문 하나를 줄였다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
StringTokenizer st;
int[] num = new int[count];
st = new StringTokenizer(br.readLine()," ");
for (int i = 0; i < num.length; i++) {
int A = Integer.parseInt(st.nextToken());
num[i] = A;
}
int max = num[0];
int min = num[0];
for(int i = 0 ; i < num.length - 1; i++) {
if(max < Math.max(num[i], num[i+1])) {
max = Math.max(num[i], num[i+1]);
}
if(min > Math.min(num[i], num[i+1])) {
min = Math.min(num[i], num[i+1]);
}
}
bw.write(min + " " + max);
br.close();
bw.flush();
bw.close();
}
}
등수로 50등정도닌깐 나쁘지 않은 거 같습니다.
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준 알고리즘][자바] 1546번 : 평균 (0) | 2021.03.04 |
---|---|
[백준 알고리즘][자바] 2562번 : 최댓값 (0) | 2021.03.02 |
[백준 알고리즘][자바] 1110번 : 더하기 사이클 (0) | 2021.01.14 |
[백준 알고리즘][자바] 10951번 : A+B - 4 (0) | 2021.01.10 |
[백준 알고리즘][자바] 10952번 : A+B -5 (0) | 2021.01.10 |
댓글