์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ / java ] 15552 - ๋น ๋ฅธ A+B ( BufferedReader )

Forest Yun 2021. 6. 30. 01:08
728x90

๋น ๋ฅธ A+B ์„ฑ๊ณต

์‹œ๊ฐ„ ์ œํ•œ :1์ดˆ

๋ฌธ์ œ

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

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

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

์ž…๋ ฅ

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

์ถœ๋ ฅ

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

 

public class baekjoon_15552 {

	public static void main(String[] args){
		int testNum;
		int fir;
		int sec;
		String result;
		
		try {
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
			BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(System.out));
			testNum=Integer.parseInt(bufferedReader.readLine());	
			
			for(int i=0; i<testNum;i++) {
				StringTokenizer stringTokenizer=new StringTokenizer(bufferedReader.readLine()); //๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ
				fir=Integer.parseInt(stringTokenizer.nextToken());
				sec=Integer.parseInt(stringTokenizer.nextToken());
				
				result=Integer.toString(fir+sec);
				
				bufferedWriter.write(result);
				bufferedWriter.newLine();
			}
			bufferedReader.close(); //๋ฒ„ํผ ์ด์šฉ์‹œ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.
			bufferedWriter.flush(); //๋ฒ„ํผ๋ฅผ ๋‹ซ๊ธฐ ์ „ ๋ฒ„ํผ ๋‚ด ๋ฐ์ดํ„ฐ๋“ค์„ ๋ชจ๋‘ ๋‚ด๋ณด๋‚ด์•ผํ•œ๋‹ค.
			//flush ๋ฉ”์†Œ๋“œ๋ฅผ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ๋งŒ ํ•ด์•ผ ์‹œ๊ฐ„ ์ดˆ๊ณผx 
			bufferedWriter.close();	// ๋ฒ„ํผ ์ด์šฉ์‹œ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.
			
		}catch(IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}

 

 




 

 

 

 

 

BufferedReader ์™€ BufferedWriter

java.IO ํŒจํ‚ค์ง€์— ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค.

๋ฒ„ํผ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…์ถœ๋ ฅ์‹œ ํšจ์œจ์ด ์ข‹๋‹ค.

์˜ˆ) ํ™์„ ์‚ฝ์œผ๋กœ ํ•œ ๋ฒˆ์”ฉ ํ’€ ๋•Œ๋งˆ๋‹ค ์˜ฎ๊ธฐ๊ธฐ 10๋ฒˆ vs ์ˆ˜๋ ˆ์— 10๋ฒˆ ์‚ฝ์œผ๋กœ ํ™์„ ํผ ๋‹ด์•„ ํ•œ ๋ฒˆ์— ์˜ฎ๊ธฐ๊ธฐ

 

๋ฒ„ํผ(buffer)
์–ด๋–ค ์žฅ์น˜์—์„œ ๋‹ค๋ฅธ ์žฅ์น˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์†ก์‹ ํ•  ๋•Œ ์ผ์–ด๋‚˜๋Š” ์‹œ๊ฐ„์˜ ์ฐจ์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์˜ ์†๋„ ์ฐจ์ด๋ฅผ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ผ์‹œ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์–ต์‹œํ‚ค๋Š” ์žฅ์น˜.
์ถœ์ฒ˜ Oxford Languages

๋ฒ„ํผ๋ง(buffering)
์–ด๋–ค ์žฅ์น˜์—์„œ ๋‹ค๋ฅธ ์žฅ์น˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์†ก์‹ ํ•  ๋•Œ ์–‘์ž์˜ ์†๋„ ์ฐจ์ด๋ฅผ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ผ์‹œ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์–ต์‹œํ‚ค๋Š” ์ˆ˜๋ฒ•

 

 

1. BufferedReader

๋ณดํ†ต ์ž๋ฐ”์—์„œ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด java.util ํŒจํ‚ค์ง€์˜ Scanner ํด๋ž˜์Šค์„ ์‚ฌ์šฉํ•œ๋‹ค. Scanner๊ฐ™์€ ๊ฒฝ์šฐ ๊ณต๋ฐฑ ๋ฌธ์ž์™€ ์—”ํ„ฐ๋กœ ์ž…๋ ฅ๊ฐ’์„ ๊ตฌ๋ถ„ํ•œ๋‹ค. ์˜ˆ) Scanner.next() / Scanner.nextLine()

์ด์™€ ๋‹ค๋ฅด๊ฒŒ BuferedReader๋Š” ์—”ํ„ฐ๋งŒ ๊ฒฝ๊ณ„๋กœ ์ธ์‹ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ณต๋ฐฑ ๋ฌธ์ž๋กœ ์ž…๋ ฅ๊ฐ’์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” StringTokenizer๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ค.)

 

Reads text from a character-input stream, buffering characters so as to * provide for the efficient reading of characters, arrays, and lines. * *
: ๋ฌธ์ž๋“ค, ๋ฐฐ์—ด, ์ค„์˜ ํšจ์œจ์ ์ธ ์ฝ๊ธฐ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฌธ์ž๋“ค์„ ๋ฒ„ํผ๋งํ•˜๋ฉด์„œ ๋ฌธ์ž ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์œผ๋กœ๋ถ€ํ„ฐ ๊ธ€์ž๋ฅผ ์ฝ๋Š”๋‹ค.
<p> The buffer size may be specified, or the default size may be used. The * default is large enough for most purposes. * *
: ๋ฒ„ํผ ํฌ๊ธฐ๋Š” ์ •ํ•  ์ˆ˜๋„, ๊ธฐ๋ณธ ํฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ธฐ๋ณธ ํฌ๊ธฐ๋Š” ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค
<p> In general, each read request made of a Reader causes a corresponding * read request to be made of the underlying character or byte stream.
:์ผ๋ฐ˜์ ์œผ๋กœ, Reader์˜ ๊ฐ ์ฝ๊ธฐ ์š”์ฒญ์€ ํ‰๊ท  ์ดํ•˜์˜ ๋ฌธ์ž, byte ์ŠคํŠธ๋ฆผ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ•ด๋‹น ์ฝ๊ธฐ์š”์ฒญ์„ ์•ผ๊ธฐ์‹œํ‚จ๋‹ค. 
It is * therefore advisable to wrap a BufferedReader around any Reader whose read() * operations may be costly, such as FileReaders and InputStreamReaders.
:๋”ฐ๋ผ์„œ BufferedReader๋กœ FileReader์™€ InputStreamReader๊ฐ™์ด read()์ž‘์—…์— ๋น„์šฉ์ด ๋งŽ์ด ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ Reader๋ฅผ ๊ฐ์‹ธ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

For * example, * * <pre> * BufferedReader in * = new BufferedReader(new FileReader("foo.in")); * </pre> * * will buffer the input from the specified file. Without buffering, each * invocation of read() or readLine() could cause bytes to be read from the * file, converted into characters, and then returned, which can be very * inefficient. * * <p> Programs that use DataInputStreams for textual input can be localized by * replacing each DataInputStream with an appropriate BufferedReader. * * @see FileReader * @see InputStreamReader * @see java.nio.file.Files#newBufferedReader * * @author Mark Reinhold * @since 1.1

 

 


 

 

BufferedReader์˜ ์‚ฌ์šฉ๋ฒ•

import java.io.BufferedReader; //BufferedReader๋Š” java.ioํŒจํ‚ค์ง€

class Test{
	public static void main(String[] args) throws IOException{
    	//BufferedReader ์‚ฌ์šฉ ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์ˆ˜.
        // try-catch ๊ตฌ๋ฌธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    	BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
        
        int intNum=Integer.parseInt(bfr.readLine());
        //BufferedReader.readLine()์€ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— 
        //int๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ˜•๋ณ€ํ™˜์ด ํ•„์š”ํ•˜๋‹ค.
        
        //๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
        StringTokenizer stz=new StringTokenizer(bfr.readLine());//๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”
        String str1 = stz.nextToken();
        String str2= stz.nextToken();
       	์˜ˆ) 
        ์ž…๋ ฅ: this is a test
        str1 -> this 
        str2 -> is
        
        bfr.close() //๋ฒ„ํผ ์ด์šฉ์‹œ ์‚ฌ์šฉ์ด ๋๋‚œ ํ›„ ๋ฐ˜๋“œ์‹œ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.
        }
}
        

 

 


 

 

๋ฉ”์„œ๋“œ ์ข…๋ฅ˜

๋ฉ”์„œ๋“œ ๋ช… ์„ค๋ช…
BufferedReader(Reader in[, int size]) Creates a buffering character-input stream that uses an input buffer of
     * the specified size.
size ํฌ๊ธฐ์˜ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž ์ž…๋ ฅ ๋ฒ„ํผ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑํ•œ๋‹ค.
- size ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒ๋žต ์‹œ defualt ํฌ๊ธฐ์˜ ๋ฌธ์ž ์ž…๋ ฅ ๋ฒ„ํผ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
int read([char cbuf[][, int off, int len]]) ๋ฌธ์ž ๋ฐฐ์—ด cbuf์„ off ์œ„์น˜์—์„œ ๋ถ€ํ„ฐ len ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ  ํ•œ ๋ฌธ์ž์”ฉ ์ฝ์–ด ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ํ•ด๋‹น ๋ฌธ์ž์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ฐ’ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ์ŠคํŠธ๋ฆผ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
-read() :Reads a single character. ํ•œ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ฝ๋Š”๋‹ค.
-read(char cbuf[])
String readLine() Reads a line of text.
'\n'์„ ๊ธฐ์ค€์œผ๋กœ, ํ•œ ์ค„์˜ ๊ธ€์„ ์ฝ๊ณ  ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
long skip(long n) Skips characters.
n ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ฑด๋„ˆ๋›ด ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean ready() Tells whether this stream is ready to be read.
ํ•ด๋‹น ์ŠคํŠธ๋ฆผ์ด ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ์•Œ๋ ค์ค€๋‹ค.
boolean markSupported() Tells whether this stream supports the mark() operation, which it does.
ํ•ด๋‹น ์ŠคํŠธ๋ฆผ์ด mark() ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€ ์•Œ๋ ค์ค€๋‹ค.
void mark(int readAheadLimit) Marks the present position in the stream.
์ŠคํŠธ๋ฆผ์—์„œ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋งˆํ‚นํ•œ๋‹ค.
void reset()  Resets the stream to the most recent mark.
๊ฐ€์žฅ ์ตœ๊ทผ ๋งˆํ‚น์œผ๋กœ ์ŠคํŠธ๋ฆผ์„ ์žฌ์„ค์ •ํ•œ๋‹ค.
void close() ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„์ค€๋‹ค.

 

 

 

 

 




 

 

 

 

 

 

๊ณต๋ถ€์— ์ฐธ๊ณ ํ•œ ์ž๋ฃŒ

https://jhnyang.tistory.com/92
https://coding-factory.tistory.com/251
https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966 
์ž๋ฐ” ๊ณต์‹ ๋ฌธ์„œ
BufferedReader , BufferedWriter
https://jhnyang.tistory.com/398
์ž๋ฐ”๊ณต์‹ ๋ฌธ์„œ
StringTokenizer

 

728x90