백준 28125 2023 아주머학교 프로그래딩 정시머힌 Java & Kotlin

: ) YOUNG·2023년 5월 30일
1

알고리즘

목록 보기
205/417
post-thumbnail

백준 28125번
https://www.acmicpc.net/problem/28125

문제




생각하기


  • 간단한 조건문을 활용하는 문자열 문제이다.

동작




코드


Java


import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int N = Integer.parseInt(br.readLine());
        for (int i = 0; i < N; i++) {
            char[] chArr = br.readLine().toCharArray();

            int passwordCnt = 0;
            int slashCount = 0;

            StringBuilder tempString = new StringBuilder();
            for (char ch : chArr) {

                if (ch == '@') {
                    passwordCnt++;
                    tempString.append('a');
                } else if (ch == '[') {
                    passwordCnt++;
                    tempString.append('c');
                } else if (ch == '!') {
                    passwordCnt++;
                    tempString.append('i');
                } else if (ch == ';') {
                    passwordCnt++;
                    tempString.append('j');
                } else if (ch == '^') {
                    passwordCnt++;
                    tempString.append('n');
                } else if (ch == '0') {
                    passwordCnt++;
                    tempString.append('o');
                } else if (ch == '7') {
                    passwordCnt++;
                    tempString.append('t');
                } else if (ch == '\\') {
                    slashCount++;
                } else if (slashCount == 1 && ch == '\'') {
                    slashCount = 0;
                    passwordCnt++;
                    tempString.append('v');
                } else if (slashCount == 1 && ch == '\\') {
                    slashCount++;
                } else if (slashCount == 2 && ch == '\'') {
                    slashCount = 0;
                    passwordCnt++;
                    tempString.append('w');
                } else {
                    // 암호가 아닌 경우
                    tempString.append(ch);
                }
            }

            int tempLen = tempString.length();
            if(tempLen % 2 == 1) {
                tempLen += 1;
            }

            if (tempLen / 2 <= passwordCnt) {
                sb.append("I don't understand").append('\n');
            } else {
                sb.append(tempString).append('\n');
            }
        }

        bw.write(sb.toString());
        bw.close();
    } // End of main
} // End of Main class


Kotlin


import java.io.*
import java.io.OutputStreamWriter

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.`out`))
    val sb = StringBuilder()

    val N = br.readLine().toInt()
    for (i in 0 until N) {
        val chArr = br.readLine().toCharArray()

        var passordCount = 0
        var slashCount = 0

        val tempString = StringBuilder()
        chArr.forEach { ch ->

            if (ch == '@') {
                passordCount++;
                tempString.append('a');
            } else if (ch == '[') {
                passordCount++;
                tempString.append('c');
            } else if (ch == '!') {
                passordCount++;
                tempString.append('i');
            } else if (ch == ';') {
                passordCount++;
                tempString.append('j');
            } else if (ch == '^') {
                passordCount++;
                tempString.append('n');
            } else if (ch == '0') {
                passordCount++;
                tempString.append('o');
            } else if (ch == '7') {
                passordCount++;
                tempString.append('t');
            } else if (ch == '\\') {
                slashCount++;
            } else if (slashCount == 1 && ch == '\'') {
                slashCount = 0;
                passordCount++;
                tempString.append('v');
            } else if (slashCount == 1 && ch == '\\') {
                slashCount++;
            } else if (slashCount == 2 && ch == '\'') {
                slashCount = 0;
                passordCount++;
                tempString.append('w');
            } else {
                // 암호가 아닌 경우
                tempString.append(ch);
            }
        }

        var tempLen = tempString.length
        if (tempLen % 2 == 1) {
            tempLen++
        }

        if (tempLen / 2 <= passordCount) {
            sb.append("I don't understand").append('\n');
        } else {
            sb.append(tempString).append('\n');
        }
    }

    bw.write(sb.toString())
    bw.close()
} // End of main


0개의 댓글