본문 바로가기

알고리즘/백준

[백준] 2739 - 구구단

반응형

조건문 단계를 무사히 마치고 반복문 단계로 진입했다!

2739번 문제

 

이 문제 역시 반복문 하면 빠지지 않고 나오는 예제다.

Scanner 클래스를 이용한 방법과 BufferedReader를 이용한 방법 2가지로 풀어봤다.

 

1. Scanner 클래스를 이용한 방법

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

        int n;

        n = sc.nextInt();
        
        for(int i=1;i<=9;i++){
            System.out.println(n + " * " + i + " = " + n*i);
        }
        sc.close();

    }
}

 

2. BufferedReader를 이용한 방법

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
    public static void main(String[] args)throws IOException{
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        for(int i=1;i<=9;i++){
            System.out.println(n + " * "+ i + " = " + n*i);
        }
    }
}

 

위 BufferedReader, 아래 Scanner

 

사용할 때마다 느끼지만 BufferedReader가 빠르다. 채점도 Scanner클래스보다 빠르게 이루어진다.

반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 8393 - 합  (0) 2024.02.01
[백준] 10950 - A + B(3)  (0) 2024.01.31
[백준] 2480 - 주사위 세 개  (0) 2024.01.29
[백준] 2525 - 오븐 시계  (0) 2024.01.28
[백준] 2884 - 알람 시계  (0) 2024.01.27