연습 문제 : https://bcp0109.tistory.com/39
자바 입력 방법은 Scanner과 BufferedReader가 있습니다.
Scanner가 쉽고 간단하여 저도 처음 문제를 풀 때는 사용하였지만, 사실 알고리즘 문제풀이에는 적절하지 않습니다.
많은 입력을 받아야 하는 경우에는 시간초과가 날 수 있기 때문입니다.
따라서 BufferedReader를 사용하는 것에 익숙해지는 것을 추천합니다.
** BufferedReader를 사용하기 위해서는 try문 내에서 사용하거나 메소드 끝에 throws Exception 구문을 붙여주셔야 합니다!
한 줄에 여러 개의 입력을 받는 경우에는 StringTokenizer 를 사용하면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
class Main { | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
StringTokenizer st; | |
// input intger 1. | |
// ex) 1 | |
int a = Integer.parseInt(br.readLine()); | |
// inpurt integer 2. | |
// ex) 1 2 | |
st = new StringTokenizer(br.readLine()); | |
int v1 = Integer.parseInt(st.nextToken()); | |
int v2 = Integer.parseInt(st.nextToken()); | |
// input String 1. | |
// ex) hello | |
String s = br.readLine(); | |
// input String 2. | |
// ex) hello world | |
st = new StringTokenizer(br.readLine()); | |
String s1 = st.nextToken(); | |
String s2 = st.nextToken(); | |
} | |
} |
'알고리즘 문제 > 공부' 카테고리의 다른 글
자바 Collections 시간복잡도 (Big-O) (0) | 2019.04.02 |
---|---|
자바 출력 (Java) (0) | 2019.01.05 |
위상정렬 Topological Sort (Java) (1) | 2018.12.28 |
부분집합 PowerSet (Java) (2) | 2018.12.27 |
조합 Combination (Java) (7) | 2018.12.27 |