언어/JAVA

JAVA 입력

hazel_ 2021. 7. 20. 21:44

 

BOJ 10818 문제

https://www.acmicpc.net/problem/10818

Scanner

package boj;

import java.util.Arrays;
import java.util.Scanner;

public class Boj_10818_Scanner {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int N = scan.nextInt();
		int[] arr = new int[N];
		int min = Integer.MAX_VALUE;
		int max = 0;
		
		for(int i=0; i<N; i++) {
			arr[i]=scan.nextInt();
		}
		Arrays.sort(arr);
		System.out.println(arr[0]+" "+arr[N-1]);
	}

}

 

BufferedReader

package boj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Boj_10818_BufferedReader{

	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 index=0;
		int[] arr = new int[N];
		while(st.hasMoreTokens()) {
			arr[index] = Integer.parseInt(st.nextToken());
			index++;
		}
		
		Arrays.sort(arr);
		System.out.println(arr[0]+" "+arr[N-1]);
	}

}

 

속도, 메모리면에서 BufferedReader가 훨씬 효율이 좋다.

scanner

BufferedReader

 

단점은 코드길이가 길어진다는 것