algorithm

๋ฐฑ์ค€ ๋ฐ˜๋ณต๋ฌธ ๋งˆ์ง€๋ง‰ 10951๋ฒˆ. A+B - 4

์ˆ˜ํ˜€์ด0812 2023. 7. 15. 21:44
728x90
๋ฐ˜์‘ํ˜•

< 10951. A+B - 4 > 

์ž…๋ ฅ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ. EOF์— ๋Œ€ํ•ด ์•Œ์•„ ๋ณด์„ธ์š”!!

 

๋ฌธ์ œ

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

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

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

์ถœ๋ ฅ

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

 

์•„๋ฌด์ƒ๊ฐ ์—†์ด ์•ž๋ฌธ์ œ๋ž‘ ๋˜‘๊ฐ™๋„ค ๋ญ์•ผ,,, ํ•˜๋ฉด์„œ ์—ด์‹ฌํžˆ ์ ์—ˆ๋Š”๋ฐ ๊ณ„์† ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”....ใ… 

๊ทธ๋ž˜์„œ ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ดค๋Š”๋ฐ ๋ฌธ์ œ ๋ฐ‘์— EOF์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ์—ฌ ;;;;

EOF๋Š” ๋˜ ๋ญ๋žŒ...

 


 

EOF ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿค”๐Ÿค”

 

EOF : ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋” ์ด์ƒ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์šฉ์–ด

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์ž…๋ ฅ๊ฐ’์„ ์–ผ๋งˆ๋‚˜ ๋ฐ›์„์ง€ ๋ช…์‹œํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!!

 

1) Scanner ์‚ฌ์šฉ ์‹œ

 

  • hasNext() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
  • ์ž…๋ ฅ๋œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

 

2) BufferReader ์‚ฌ์šฉ์‹œ

 

  • null ์ด ์•„๋‹˜์„ ๋ช…์‹œํ•ด ์ค๋‹ˆ๋‹ค!!

 

ํŒŒ์ด์ฌ๊ณผ C++ ์–ธ์–ด ์‚ฌ์šฉ์˜ ๊ฒฝ์šฐ์— ๋‹ค ๋‹ค๋ฅด๋ฏ€๋กœ ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด ๋ณ„ EOF๋ฅผ ๊ฒ€์ƒ‰ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜‚

 


import java.util.Scanner;
 
public class Main {
	public static void main(String args[]){
		
		Scanner in=new Scanner(System.in);
			
		while(in.hasNextInt()){
		
			int a=in.nextInt();
			int b=in.nextInt();
			System.out.println(a+b);
		}	
		in.close();
	}
}

 

์›๋ž˜๋ผ๋ฉด while(true)๋ฅผ ์จ์ค„๊ป€๋ฐ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ ์ˆ˜ ์—†์œผ๋‹ˆ๊นŒ while(in.hasNextInt())๋ฅผ ์‚ฌ์šฉํ•ด์คฌ๋‹ค

์ง„์งœ ๋ช‡๋ฒˆ ํ‹€๋ ธ๋Š”์ง€ ์…€ ์ˆ˜๋Š” ์žˆ์ง€๋งŒใ…‹ ๋งŽ์ด ํ‹€๋ ธ๋‹ค..

 

 

์ด๊ฑฐ BufferReader๋กœ ๋ฐ”๊พธ๋ฉด ๋˜ ๋งŽ์ด ํ‹€๋ฆด๊ฒƒ ๊ฐ™์•˜์ง€๋งŒ ํ•ด๋ด์•ผํ•  ๊ฑฐ ๊ฐ™์•„์„œ ์ผ๋‹จ ํ•ด๋ดค๋‹ค.

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

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		
		while (true) {
			String str = br.readLine();
			if (str == null) {
				break;
				
			}
			
			st = new StringTokenizer(str);
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			bw.write(a + b + "\n");
		}
		bw.close();
	}
}

 

๊ฐ’์ด ์—†์œผ๋ฉด ๋ฉˆ์ถฐ์•ผ ํ•˜๋‹ˆ๊นŒ str == null์„ ์‚ฌ์šฉํ•ด์คฌ๋‹ค.

 

๋ฉ”๋ชจ๋ฆฌ๋‚˜ ์‹œ๊ฐ„์ ์œผ๋กœ๋‚˜ ์ ๊ฒŒ ๊ฑธ๋ ค์„œ ๋ฒ„ํผ๋ฆฌ๋” ์“ฐ๋Š” ์—ฐ์Šต์„ ์—ด์‹ฌํžˆ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.. ์‹ค์ œ๋กœ ์ž๋ฐ”๊ฐ€ ๋‹ค๋ฅธ ์–ธ์–ด๋ณด๋‹ค ๋Š๋ฆฌ๋‹ค๊ณ  ํ•˜๋‹ˆ ๋”๋”์šฑ ๋ฒ„ํผ ์—ฐ์Šตํ•ด์•ผํ•  ๋“ฏ

 

 

728x90
๋ฐ˜์‘ํ˜•