본문 바로가기
알고리즘 공부/백준

[JAVA 백준 17일차 / 하루 3문제] 5622번, 11718번, 25083번

by maverick11471 2025. 2. 18.

1. 5622번 다이얼

 가. Stream 사용 안하는 경우

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        // BufferedReader로 입력 받기
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        br.close();
        
        int totalTime = 0;
        
        // 각 문자마다 해당하는 시간을 누적
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch >= 'A' && ch <= 'C') {
                totalTime += 3;
            } else if (ch >= 'D' && ch <= 'F') {
                totalTime += 4;
            } else if (ch >= 'G' && ch <= 'I') {
                totalTime += 5;
            } else if (ch >= 'J' && ch <= 'L') {
                totalTime += 6;
            } else if (ch >= 'M' && ch <= 'O') {
                totalTime += 7;
            } else if (ch >= 'P' && ch <= 'S') {
                totalTime += 8;
            } else if (ch >= 'T' && ch <= 'V') {
                totalTime += 9;
            } else if (ch >= 'W' && ch <= 'Z') {
                totalTime += 10;
            }
        }
        
        // 결과 출력
        System.out.println(totalTime);
    }
}

 

 나. Stream 사용하는 경우

import java.io.*;
import java.util.*;
import java.util.stream.*;

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

        // 알파벳별 다이얼 매핑 (문제에 따른 숫자 할당)
        Map<Character, Integer> dialMap = Map.ofEntries(
            Map.entry('A', 2), Map.entry('B', 2), Map.entry('C', 2),
            Map.entry('D', 3), Map.entry('E', 3), Map.entry('F', 3),
            Map.entry('G', 4), Map.entry('H', 4), Map.entry('I', 4),
            Map.entry('J', 5), Map.entry('K', 5), Map.entry('L', 5),
            Map.entry('M', 6), Map.entry('N', 6), Map.entry('O', 6),
            Map.entry('P', 7), Map.entry('Q', 7), Map.entry('R', 7), Map.entry('S', 7),
            Map.entry('T', 8), Map.entry('U', 8), Map.entry('V', 8),
            Map.entry('W', 9), Map.entry('X', 9), Map.entry('Y', 9), Map.entry('Z', 9)
        );

        // 각 문자를 숫자로 매핑한 후, 전화 걸기 시간을 계산
        int totalTime = input.chars()
            .mapToObj(c -> (char) c)      // int 스트림을 Character 스트림으로 변환
            .map(dialMap::get)            // 매핑된 숫자 가져오기
            .mapToInt(n -> n + 1)         // 다이얼 숫자에 1을 더해 걸리는 시간 계산
            .sum();                      // 총 시간 합산

        System.out.println(totalTime);
    }
}
  • 이 문제는 오히려 Stream을 사용하지 않은 정답안이 더 간략하다.

2. 11718번 그대로 출력하기

 가. Stream 사용하지 않는 경우

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        // 입력이 더 이상 없을 때까지 한 줄씩 읽어 출력
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

 

 나. Stream 사용하는 경우

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // BufferedReader의 lines() 메서드를 이용하여 스트림을 생성한 후 각 줄을 출력
        br.lines().forEach(System.out::println);
    }
}

3. 25083번 새싹

 가. Stream 사용하지 않는 경우

public class Main {
    public static void main(String[] args) {
        System.out.println("         ,r'\"7");
        System.out.println("r`-_   ,'  ,/");
        System.out.println(" \\. \". L_r'");
        System.out.println("   `~\\/");
        System.out.println("      |");
        System.out.println("      |");
    }
}

 

 나. Stream 사용하는 경우

import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream.of(
            "         ,r'\"7",
            "r`-_   ,'  ,/",
            " \\. \". L_r'",
            "   `~\\/",
            "      |",
            "      |"
        ).forEach(System.out::println);
    }
}