💡문제 풀이/백준 - JAVA

A+B - 8 / 11022

뇌 리셋은 기본이지 2023. 11. 16. 23:51

정보

분류 : 반복문

 

문제

문제링크

 

풀이

  • 풀이 1 (Scanner + println()) → 276ms
import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();

		for(int i = 1 ; i <= a ; i++) {
			int b = sc.nextInt();
			int c = sc.nextInt();
            
			System.out.println("Case #" + i + ": "+ b + " + " + c + " = " + (b + c));
		}
	}
}
  • 풀이 2 (BufferedReader + BufferedWriter + StringTokenizer) → 180ms
import java.io.*;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		// InputStream : 자바의 가장 기본이 되는 입력 스트림
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		// OutputStreamWriter : 자바의 가장 기본이 되는 출력 스트림
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int a = Integer.parseInt(br.readLine());
		
		StringTokenizer str;

		for(int i = 1 ; i <= a ; i++) {
			str = new StringTokenizer(br.readLine()," ");
			
			int b = Integer.parseInt(str.nextToken());
			int c = Integer.parseInt(str.nextToken());
			
			bw.write("Case #" + i + ": "+ b + " + " + c + " = " + (b + c) + "\n");
		};
		
		br.close();
		
		bw.flush();
		bw.close();
	}
}

 

풀이 후기

StringTokeniazer 클래스 사용하는게 익숙하지 않다. Scanner 대신 사용하다보면 손에 익을 거 같다. 

'💡문제 풀이 > 백준 - JAVA' 카테고리의 다른 글

별 찍기 - 2 / 2439  (0) 2023.11.17
별 찍기 - 1 / 2438  (0) 2023.11.17
A+B - 7 / 11021  (0) 2023.11.16
빠른 A+B / 15552  (0) 2023.11.16
코딩은 체육과목 입니다 / 25314  (0) 2023.11.16