알고리즘
백준 1000번 자바 A+B 해결방법 2가지
GOMSHIKI
2023. 1. 5. 16:56
반응형
백준 1000번 A+B 문제
해결방법 1) Scanner 이용하기
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.next());
int d = Integer.parseInt(sc.next());
System.out.println(c+d);
}
}
해결방법 2) BufferedReader를 이용하기
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
/** 스트림 : 입력장치와 프로그램 사이 단방향으로 연결해주는 역할 **/
/** InputStream : 입력받은 데이터를 바이트단위로 읽어드림(=Byte Stream) **/
/** InputStreamReader : 문자단위로 읽어드림 (=Character Stream) **/
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
/** 입력받은 값을 스페이스(" ") 기준으로 나눠 가지고있음 **/
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
/** 나눠진 입력값을 Int로 타입변경 후 변수에 담기 **/
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
/** 결과 출력 **/
System.out.println(a+b);
bf.close();
}
}
결과 비교
방법1 (Scanner) | 방법2 (BufferedReader) | |
사용메모리 | 17736 KB | 14256 KB |
시간 | 212 ms | 128 ms |
반응형