๐Ÿ’ป๋ฌธ์ œ ํ’€์ด - [Java] 5622. ๋‹ค์ด์–ผ

์ดํ•˜์–€ยท2024๋…„ 2์›” 1์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
5/33

๐Ÿ“๋ฌธ์ œ ์ถœ์ฒ˜ : https://www.acmicpc.net/problem/5622
๐Ÿ“code ์ €์žฅ์†Œ
hayannn/CodingTest_Java_NKLCBHub/#5622 ๋‹ค์ด์–ผ1,3
hayannn/CodingTest_Java_NKLCBHub/#5622 ๋‹ค์ด์–ผ2


๐Ÿค” ๋ฌธ์ œ ์š”์•ฝ

์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ๋ˆ„๋ฅด๋ฉด ๋‹ค์ด์–ผ์ด ์ฒ˜์Œ ์œ„์น˜๋กœ ๋Œ์•„๊ฐˆ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๋‹ค์Œ ์ˆซ์ž๋ฅผ ๋ˆ„๋ฅด๋Š” ๋‹ค์ด์–ผ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

  • ๊ทœ์น™

    • ์ˆซ์ž 1์€ ์ด 2์ดˆ๊ฐ€ ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.
    • 1๋ณด๋‹ค ํฐ ์ˆ˜์˜ ์‹œ๊ฐ„์€ ํ•œ ์นธ ์˜†์— ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๊ฑธ๊ธฐ ์œ„ํ•ด 1์ดˆ์”ฉ ๋” ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.(+1)
    • ์ž…๋ ฅ์€ ๋ฌธ์ž๋กœ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ์ด ์ „ํ™”๋ฅผ ๊ฑธ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

    • ์ˆซ์ž&๋ฌธ์ž ๋Œ€์นญ
    ์ˆซ์ž๋ฌธ์ž์†Œ์š”์‹œ๊ฐ„
    1
    2ABC3s
    3DEF4s
    4GHI5s
    5JKL6s
    6MNO7s
    7PQRS8s
    8TUV9s
    9WXYZ10s
    011s
  • ๋ฌธ์ œ๋ฅผ ๊ต‰์žฅํžˆ ๋ณต์žกํ•˜๊ฒŒ ์„ค๊ณ„ํ–ˆ์ง€๋งŒ, ๊ฒฐ๊ตญ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฌธ์ž์— ๋”ฐ๋ฅธ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

  • ์˜ˆ์‹œ

    • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๊ฐ€ A,B,C ์ค‘ ํ•˜๋‚˜์— ์†ํ•˜๋ฉด ์šฐ์„  3์„ ๊ฐ€์ง€๊ฒŒ ๋˜๊ณ ,
    • ๊ทธ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ D,E,F ์ค‘ ํ•˜๋‚˜์— ์†ํ•˜๋ฉด ์œ„์—์„œ ์–ป์—ˆ๋˜ 3์— ์ง€๊ธˆ ์–ป์€ 4๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“ ์˜ˆ์ œ ์ž…๋ ฅ ๋ถ„์„

  • Case 1 : ๋‹จ์–ด์— ๋Œ€์‘๋˜๋Š” ์ˆซ์ž๋ฅผ ์ฐพ๊ณ , ์‹œ๊ฐ„์„ ๋ชจ๋‘ ๋”ํ•ด์ฃผ๊ธฐ

๐Ÿšจ ์ฃผ์˜์‚ฌํ•ญ

  • ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 2<=x<=15



1๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜: switch - case๋ฌธ ํ™œ์šฉ

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

์ˆœ์„œ

  • ๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ์˜ˆ์™ธ1) ๋‹จ์–ด ๊ธธ์ด 2 <= x <= 15 || ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋งŒ
  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฅผ ์†Œ์š”์‹œ๊ฐ„์— ๋Œ€์น˜ํ•˜๊ธฐ
  • ์†Œ์š”์‹œ๊ฐ„ ๋ชจ๋‘ ํ•ฉํ•ด์„œ ์ถœ๋ ฅ
package baekjoon.level.L5;

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

		// 1. ๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ
        String words = scanner.nextLine();

		// 1-1. ์˜ˆ์™ธ : ๋‹จ์–ด ๊ธธ์ด 2 <= x <= 15 || ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋งŒ
        if(words.length() < 2 || words.length() > 15 || !isAllUpperCase(words)){
            System.out.print("๋‹จ์–ด๋Š” 2 ์ด์ƒ 15์ดํ•˜๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋ฉฐ, ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
        } else {
        
            int count = 0;
            int length = words.length();
            
            // ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฅผ ์†Œ์š”์‹œ๊ฐ„์— ๋Œ€์น˜ํ•˜๊ธฐ
            for (int i = 0; i < length; i++) {
                switch (words.charAt(i)) { //charAt(string ๋ฌธ์ž์—ด ์ค‘ ํ•œ๊ธ€์ž๋ฅผ char ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜)
                case 'A':
                case 'B':
                case 'C':
                    count += 3;
                    break;

                case 'D':
                case 'E':
                case 'F':
                    count += 4;
                    break;

                case 'G':
                case 'H':
                case 'I':
                    count += 5;
                    break;

                case 'J':
                case 'K':
                case 'L':
                    count += 6;
                    break;

                case 'M':
                case 'N':
                case 'O':
                    count += 7;
                    break;

                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                    count += 8;
                    break;

                case 'T':
                case 'U':
                case 'V':
                    count += 9;
                    break;

                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                    count += 10;
                    break;
                }
            }
            
            //์ฐจ๋ก€๋กœ ๋ฌธ์ž์— ๋งž๋Š” ์ˆซ์ž๋ฅผ ๋”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— count๋ผ๊ณ  ํ•˜๋ฉด ์ด ํ•ฉ์ด ๋‚˜์˜ค๊ฒŒ ๋จ.
            System.out.println(count);
        }
    }
    
    //1-1. ์˜ˆ์™ธ ) ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ
    private static boolean isAllUpperCase(String string){
        for(char c : string.toCharArray()){
            if(!Character.isUpperCase(c)){
                return false;
            }
        }
        return true;
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • switch(words.charAt(i)

    • ์œ„์—์„œ String ํ˜•ํƒœ๋กœ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•˜๊ณ , ์•ŒํŒŒ๋ฒณ ํ•œ ๊ธ€์ž์”ฉ ๋–ผ์–ด๋‚ด์–ด ์–ด๋–ค Case์— ์†ํ•˜๊ฒŒ ๋˜๋Š”์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
    • charAt์€ String ๋ฌธ์ž์—ด ์ค‘ 1๊ธ€์ž๋ฅผ char ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • count += 3;

    • ์•ŒํŒŒ๋ฒณ case ๋ณ„๋กœ ์ด ๋ช‡ ์ดˆ๊ฐ€ ์†Œ์š”๋˜๋Š”์ง€์— ๋”ฐ๋ผ (์•ž์„  ๋‹จ์–ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ทธ count์™€) count๋ฅผ ๋”ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— 1๋ณด๋‹ค ํฐ ์ˆ˜์˜ ๊ฒฝ์šฐ, ํ•œ ์นธ ์˜†์— ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๊ฑธ๊ธฐ ์œ„ํ•ด 1์ดˆ์”ฉ ๋” ๊ฑธ๋ฆฌ๋‹ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 3,4,5... ์ด๋ ‡๊ฒŒ 1์ดˆ์”ฉ ๋”ํ•ด์ฃผ์–ด count์˜ case๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค.




2๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜: A์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด ์ˆซ์ž๋กœ ๋ณ€ํ˜•

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

์ˆœ์„œ

  • ๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ์˜ˆ์™ธ1) ๋‹จ์–ด ๊ธธ์ด 2 <= x <= 15 || ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋งŒ
  • A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์ด A์™€ ์–ผ๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•ด ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ˜•
  • ์†Œ์š”์‹œ๊ฐ„ ๋ชจ๋‘ ํ•ฉํ•ด์„œ ์ถœ๋ ฅ
package baekjoon.level.L5;

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

        String words = scanner.nextLine().toUpperCase();

        // ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 2๊ธ€์ž ์ด์ƒ 15๊ธ€์ž ์ดํ•˜์ธ์ง€ ํ™•์ธ
        if (words.length() < 2 || words.length() > 15) {
            System.out.println("2๊ธ€์ž ์ด์ƒ 15๊ธ€์ž ์ดํ•˜์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
            return;
        }

        int count = 0;

        for (int i = 0; i < words.length(); i++) {
            char character = words.charAt(i);
            int wtonumber = character - 'A'; //๊ธฐ์ค€

            if (wtonumber < 3) count += 3;
            else if (wtonumber < 6) count += 4;
            else if (wtonumber < 9) count += 5;
            else if (wtonumber < 12) count += 6;
            else if (wtonumber < 15) count += 7;
            else if (wtonumber < 19) count += 8;
            else if (wtonumber < 21) count += 9;
            else if (wtonumber <= 26) count += 10;
        }
        System.out.println(count);
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • int wtonumber = character - 'A';
    • A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ์–ผ๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค(A๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘).
    • ๊ธฐ์ค€



3๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜: A์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด ์ˆซ์ž๋กœ ๋ณ€ํ˜•(๊ฐ„๋‹จ ๋ฒ„์ „)

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

์ˆœ์„œ

  • ๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ์˜ˆ์™ธ1) ๋‹จ์–ด ๊ธธ์ด 2 <= x <= 15 || ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋งŒ
  • A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์ด A์™€ ์–ผ๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•ด ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ˜•
  • ์†Œ์š”์‹œ๊ฐ„ ๋ชจ๋‘ ํ•ฉํ•ด์„œ ์ถœ๋ ฅ
package baekjoon.level.L5;

import java.util.Scanner;

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

        String words = scanner.nextLine().toUpperCase();

        // ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 2๊ธ€์ž ์ด์ƒ 15๊ธ€์ž ์ดํ•˜์ธ์ง€ ํ™•์ธ
        if (words.length() < 2 || words.length() > 15) {
            System.out.println("2๊ธ€์ž ์ด์ƒ 15๊ธ€์ž ์ดํ•˜์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
            return;
        }

        int count = 0;

        for (int i = 0; i < words.length(); i++) {
            char character = words.charAt(i);
            int wtonumber = character - 'A';

            count += (wtonumber / 3) + 3;
            if (character == 'S' || character == 'V' || character == 'Y' || character == 'Z') {
                count -= 1;
            }
        }
        System.out.print(count);
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • count += (wtonumber / 3) + 3;
    • A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ์–ผ๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค(A๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘).
    • ๊ธฐ์ค€ ๋™์ผ
    • ๊ฐ ๋‹ค์ด์–ผ์ด ๋Œ€๋ถ€๋ถ„ ์ด 3๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ์”ฉ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ๊ณ„์‚ฐ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋งˆ์ง€๋ง‰์— 3-4-3 ํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ -1์„ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.




๐Ÿ–จ๏ธ ๊ฒฐ๊ณผ ๋น„๊ต ๋ฐ ๋ถ„์„

switch-case๋ฌธ

  • ๋ฉ”๋ชจ๋ฆฌ : 17676KB
  • ์‹œ๊ฐ„ : 204ms
  • ์ฝ”๋“œ ๊ธธ์ด : 2103B

A์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด ์ˆซ์ž๋กœ ๋ณ€ํ˜•

  • ์ด์œ  ๋ถ„์„์ค‘.. ์ธํ…”๋ฆฌ์ œ์ด์—์„œ๋Š” ์—๋Ÿฌ๋‚˜ ๊ฒฝ๊ณ  ์—†์ด ์ž˜ ๋Œ์•„๊ฐ€๋Š”๋ฐ ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

A์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด ์ˆซ์ž๋กœ ๋ณ€ํ˜•(๊ฐ„๋‹จ ๋ฒ„์ „)![]

  • ๋ฉ”๋ชจ๋ฆฌ : 17588KB
  • ์‹œ๊ฐ„ : 204ms
  • ์ฝ”๋“œ ๊ธธ์ด : 895B

โžก๏ธ ์„ฑ๋Šฅ ๋น„๊ต : 1๋ฒˆ๊ณผ 3๋ฒˆ์˜ ๊ฒฝ์šฐ, ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•ด์กŒ์ง€๋งŒ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์‹œ๊ฐ„ ์†Œ์š”๋Š” ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค!

  • ์‹œ๊ฐ„ ์ œํ•œ์ด ์งง์€ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” 1๋ฒˆ์œผ๋กœ ํ‘ธ๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.
profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€