728x90
300x250
Scanner를 통해 입력받고, System.out.println으로 출력하였을 때 시간초과가 난 코드
package algorithm.boj;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Fun11728 {
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("input.txt"));
Scanner sc = new Scanner(System.in);
int arr[] = new int[sc.nextInt() + sc.nextInt()];
for (int i = 0; i < arr.length; i++) {
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
for(int i : arr) {
System.out.print(i+" ");
}
System.out.println();
}
}
BufferedReader와 BufferedWriter를 이용해서 푸니 성공
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
sb.append(br.readLine());
StringTokenizer st = new StringTokenizer(sb.toString());
int arr[] = new int[Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())];
int idx = 0;
st = new StringTokenizer(br.readLine());
while(st.hasMoreTokens()) {
arr[idx++] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
while(st.hasMoreTokens()) {
arr[idx++] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i : arr) {
bw.write(i +" ");
}
bw.flush();
bw.close();
}
}
728x90
'알고리즘 > 분할정복' 카테고리의 다른 글
백준 2448번: 별 찍기 - 11 [분할정복][재귀] - Java (0) | 2020.09.08 |
---|---|
백준 2447번: 별 찍기 - 10 [분할정복][재귀] - Java (0) | 2020.09.06 |
백준 1992번: 쿼드트리 [분할정복][재귀] - Java (0) | 2020.09.06 |
백준 11729번: 하노이 탑 이동 순서 [분할정복][재귀] - Java (0) | 2020.09.06 |
백준 1780번: 종이의 개수 [분할정복] - Java (0) | 2020.09.06 |