[백준] 10950번 A+B - 3 -JAVA

Dev_JDra·2021년 12월 3일
0

백준

목록 보기
3/9

※주의

  • A+B의 합은 입력한 숫자만큼 반복해서 출력
  • 공백 단위로 입력한 문자열 분리

  • 접근방법
    1. 입력한 숫자만큼 반복해서 출력해야 하므로 입력한 숫자만큼 반복문을 돌려준다.
    2. 반복문 안에서 두개 의 숫자를 입력받아 A+B 해준뒤 출력한다.

  • 풀이
import java.io.*;
import java.util.StringTokenizer;

public class B10950 {

    public static void main(String args[]) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int num = Integer.parseInt(bf.readLine());

        for(int i=0; i< num; i++){

            String AB = bf.readLine();
            //readLine은 한 행을 전부 읽기때문에 공백단위로 입력한 문자열을 분리 해주어야 한다.
            StringTokenizer st = new StringTokenizer(AB, " ");
            //StringTokenizer 클래스 를 이용해 분리 해준다.
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());
            int result = A+B;

            bw.write(String.valueOf(result));
            bw.write("\n");

        }
        bw.flush();
    }
}

0개의 댓글