티스토리 뷰

백준 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

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함