[BaekJoon] 10950๋ฒˆ - A+B-3 (java)

1. ๋ฌธ์ œ

๐Ÿ‘‰ ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

- ์กฐ๊ฑด

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ
1์ดˆ 256MB


- ๋ฌธ์ œ

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

- ์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

- ์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

- ์˜ˆ์ œ

ย ย ์ž…๋ ฅย ย  ย ย  ์ถœ๋ ฅย ย 
5
1 ย ย 1
2 ย ย 3
3 ย ย 4
9 ย ย 8
5 ย ย 2
2
5
7
17
7





2. ํ’€์ด

๋ฐ˜๋ณต๋ฌธ for๋ฅผ ์‚ฌ์šฉํ•ด ์—ฐ์‚ฐ์„ ์ถœ๋ ฅํ•œ๋‹ค.

for (์ดˆ๊ธฐ์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹) {
    ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ๋™์•ˆ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ช…๋ น๋ฌธ ์ž‘์„ฑ;
}




2๊ฐœ์˜ ์ž…๋ ฅ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

  1. Scanner ํด๋ž˜์Šค
  2. BufferedReader ํด๋ž˜์Šค




- 1. Scanner ํด๋ž˜์Šค

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int T = sc.nextInt();
        
        for(int i=0;i<T;i++){
            int A = sc.nextInt();
            int B = sc.nextInt();

            System.out.println(A+B);
        }
    }
}

์œ„์™€๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, Scanner(System.in) ์—์„œ System.in ์€ ์ž…๋ ฅํ•œ ๊ฐ’์„ Byte ๋‹จ์œ„๋กœ ์ฝ๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

intํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด nextInt() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›์€ T ๋งŒํผ for๋ฌธ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.



[์—ฌ๊ธฐ์„œ ์ž ๊น!]

Scanner์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ๊ณ ์‹ถ๋‹ค๋ฉด ์—ฌ๊ธฐ ๋ฅผ ํด๋ฆญํ•˜์„ธ์š”




- 2. BufferedReader ํด๋ž˜์Šค

BufferedReader๋Š” readLine() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ•œ ํ–‰์„ ์ „๋ถ€ ์ฝ๋Š”๋‹ค.
์ด๋ฅผ ๊ณต๋ฐฑ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋‘๊ฐ€์ง€ ๋ถ„๋ฆฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž.



ย ย ย ย  โ‘  StringTokenizer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•
ย ย ย ย  โ‘ก split() ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•



โ‘  StringTokenizer ํด๋ž˜์Šค๋ฅผ ์ด์šฉ

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

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int T = Integer.parseInt(br.readLine());
        
        for(int i=0;i<T;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());
            System.out.println(A+B);
        }
        
        br.close();
    }
}

readLine()์€ ํ•œ ์ค„์„ ํ†ต์งธ๋กœ ์ฝ์–ด String์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Integer.parseInt()์„ ์‚ฌ์šฉํ•ด intํ˜•์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ค€๋‹ค.

StringTokenizer ํด๋ž˜์Šค๋Š” ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š”๋ฐ, ์ด๋•Œ ๋‚˜๋ˆ ์ง„ ๋ฌธ์ž์—ด ๊ฐ๊ฐ์„ ํ† ํฐ์ด๋ผ๊ณ  ํ•œ๋‹ค.

StringTokenizer ์˜ nextToken() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ† ํฐ์„ ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœํ•œ๋‹ค.



[์—ฌ๊ธฐ์„œ ์ž ๊น!]

BufferedReader๏ผBufferedWriter ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๊ณ  ์‹ถ์œผ๋ฉด ์—ฌ๊ธฐ ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋œ๋‹ค.





โ‘ก split() ์„ ์‚ฌ์šฉ

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int T = Integer.parseInt(br.readLine());
        
        for(int i=0;i<T;i++){
            String[] arr = br.readLine().split(" ");
            int A = Integer.parseInt(arr[0]);
            int B = Integer.parseInt(arr[1]);
            System.out.println(A+B);
        }
        
        br.close();
    }
}

readLine()์„ ํ†ตํ•ด ํ•œ ์ค„๋กœ ๋ฐ›์€ String ๋ฐ์ดํ„ฐ๋ฅผ split(" ") ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋‚˜๋ˆˆ ๋’ค ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.




3. Scanner VS BufferedReader ์„ฑ๋Šฅ ๋น„๊ต

image

์ž…๋ ฅ์˜ ๊ฒฝ์šฐ ํ™•์‹คํžˆ Scanner ๋ณด๋‹ค๋Š” BufferedReader ๊ฐ€ ๋น ๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.





๊ด€๋ จ ํŽ˜์ด์ง€

Categories:

Updated:

Leave a comment