์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step3] 15552 : ๋น ๋ฅธ A+B (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 7. 20:43

www.acmicpc.net/problem/15552

 

15552๋ฒˆ: ๋น ๋ฅธ A+B

์ฒซ ์ค„์— ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. T๋Š” ์ตœ๋Œ€ 1,000,000์ด๋‹ค. ๋‹ค์Œ T์ค„์—๋Š” ๊ฐ๊ฐ ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A์™€ B๋Š” 1 ์ด์ƒ, 1,000 ์ดํ•˜์ด๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

๋ณธ๊ฒฉ์ ์œผ๋กœ for๋ฌธ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค. ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์ด ๋Š๋ฆฌ๋ฉด ์—ฌ๋Ÿฌ ์ค„์„ ์ž…๋ ฅ๋ฐ›๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

C++์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  cin/cout์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, cin.tie(NULL)๊ณผ sync_with_stdio(false)๋ฅผ ๋‘˜ ๋‹ค ์ ์šฉํ•ด ์ฃผ๊ณ , endl ๋Œ€์‹  ๊ฐœํ–‰๋ฌธ์ž(\n)๋ฅผ ์“ฐ์ž. ๋‹จ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋” ์ด์ƒ scanf/printf/puts/getchar/putchar ๋“ฑ C์˜ ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

Java๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, Scanner์™€ System.out.println ๋Œ€์‹  BufferedReader์™€ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. BufferedWriter.flush๋Š” ๋งจ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

Python์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, input ๋Œ€์‹  sys.stdin.readline์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์ด๋•Œ๋Š” ๋งจ ๋์˜ ๊ฐœํ–‰๋ฌธ์ž๊นŒ์ง€ ๊ฐ™์ด ์ž…๋ ฅ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ .rstrip()์„ ์ถ”๊ฐ€๋กœ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๋˜ํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์€ ๋ณ„๊ฐœ์ด๋ฏ€๋กœ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์ „๋ถ€ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ €์žฅํ•œ ๋’ค ์ „๋ถ€ ์ถœ๋ ฅํ•  ํ•„์š”๋Š” ์—†๋‹ค. ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ•˜๋‚˜ ๋ฐ›์€ ๋’ค ํ•˜๋‚˜ ์ถœ๋ ฅํ•ด๋„ ๋œ๋‹ค.

์ž์„ธํ•œ ์„ค๋ช… ๋ฐ ๋‹ค๋ฅธ ์–ธ์–ด์˜ ๊ฒฝ์šฐ๋Š” ์ด ๊ธ€์— ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค.

์ด ๋ธ”๋กœ๊ทธ ๊ธ€์—์„œ BOJ์˜ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. T๋Š” ์ตœ๋Œ€ 1,000,000์ด๋‹ค. ๋‹ค์Œ T์ค„์—๋Š” ๊ฐ๊ฐ ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A์™€ B๋Š” 1 ์ด์ƒ, 1,000 ์ดํ•˜์ด๋‹ค.

 

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ

5

1 1

12 34

5 500

40 60

1000 1000

 

์˜ˆ์ œ ์ถœ๋ ฅ

2

46

505

100

2000

 


ํ’€์ด

1. Scanner ํด๋ž˜์Šค๋‚˜ System.out.println() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค.

2. Scanner์™€ System.out.println ๋Œ€์‹  BufferedReader์™€ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…์ถœ๋ ฅ ํ•œ๋‹ค.

    => BufferedReader/BufferedWriter ํด๋ž˜์Šค ์‚ฌ์šฉ

 

import java.io.*;
import java.util.StringTokenizer;

public class B15552 {

	public static void main(String[] args) throws NumberFormatException, IOException {
	
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int num = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < num; i++) {
			
			StringTokenizer st = new StringTokenizer(br.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			int c = a+b;
			bw.write(c + "\n");
		}
		
		bw.flush();
		bw.close();
	}
}