[JAVA] 백준 6단계

2024. 9. 9. 22:19코딩 테스트(Coding Test)/백준

25083 새싹

나의 답) 100ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();
        sb.append("         ,r\'\"7\n").append("r`-_   ,\'  ,/\n").append(" \\. \". L_r\'\n").append("   `~\\/\n").append("      |\n").append("      |");
        System.out.print(sb);
    }
}

3003 킹, 퀸, 룩, 비숍, 나이트, 폰

나의 답) 104ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        StringBuilder sb = new StringBuilder();
        int[] arr = {1, 1, 2, 2, 2, 8};

        st = new StringTokenizer(br.readLine());

        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] - Integer.parseInt(st.nextToken());
            sb.append(arr[i]).append(" ");
        }
        System.out.print(sb);
    }
}

2444 별 찍기 0 7

나의 답) 108ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                sb.append(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                sb.append("*");
            }
            sb.append("\n");
        }
        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j < n - i - 1; j++) {
                sb.append(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                sb.append("*");
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

10988 팰린드롬인지 확인하기

나의 답) 104ms

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

        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) == str.charAt(str.length() - i - 1)) continue;
            else {
                System.out.print("0");
                return;
            }
        }
        System.out.print("1");
    }
}

 

다른 사람의 답1) 100ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] arr = br.readLine().toCharArray();
        
        for (int i = 0; i < arr.length / 2; i++) {
            if (arr[i] != arr[arr.length - i - 1]) {
                System.out.print("0");
                return;
            }
        }
        System.out.print("1");
    }
}

 

다른 사람의 답2) 100ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        StringBuilder sb = new StringBuilder(input);
        String reverseStr = sb.reverse().toString();
        
        System.out.print(input.equals(reverseStr) ? 1 : 0);
    }
}

1157 단어 공부

나의 답) 240ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine().toUpperCase();
        int[] freq = new int[26];
        
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            freq[ch - 'A']++;
        }
        
        int maxFreq = -1;
        char maxChar = '?';
        boolean isDuplicate = false;
        
        for (int i = 0; i < 26; i++) {
            if (freq[i] > maxFreq) {
                maxFreq = freq[i];
                maxChar = (char) (i + 'A');
                isDuplicate = false;
            } else if (freq[i] == maxFreq) {
                isDuplicate = true;
            }
        }
        
        if (isDuplicate) System.out.print("?");
        else System.out.print(maxChar);
    }
}

2941 크로아티아 알파벳

나의 답) 틀림

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int ans = 0;
        String str = br.readLine();
        
        while (true) {
            if (str.contains("c=")) {
                ans++;
                str = str.replace("c=", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("c-")) {
                ans++;
                str = str.replace("c-", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("dz=")) {
                ans++;
                str = str.replace("dz=", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("d-")) {
                ans++;
                str = str.replace("d-", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("lj")) {
                ans++;
                str = str.replace("lj", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("nj")) {
                ans++;
                str = str.replace("nj", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("s=")) {
                ans++;
                str = str.replace("s=", "");
            } else {
                break;
            }
        }
        while (true) {
            if (str.contains("z=")) {
                ans++;
                str = str.replace("z=", "");
            } else {
                break;
            }
        }
        
        System.out.print(ans);
    }
}

 

수정한 답) 100ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        
        String[] croatia = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
        
        for (String c : croatia) {
            str = str.replace(c, "*");
        }
        System.out.print(str.length());
    }
}

1316 그룹 단어 체커

나의 답) 100ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int ans = 0;
        
        for (int i = 0; i < n; i++) {
            String word = br.readLine();
            if (isGroupWord(word)) ans++;
        }
        System.out.print(ans);
    }
    public static boolean isGroupWord(String word) {
        boolean[] visited = new boolean[26];
        char prev = ' ';
        
        for (int i = 0; i < word.length(); i++) {
            char current = word.charAt(i);
            
            if (current != prev && visited[current - 'a']) return false;
            
            visited[current - 'a'] = true;
            prev = current;
        }
        return true;
    }
}

 

 


25206 너의 평점은

나의 답) 100ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        double totalScore = 0;
        double totalCredit = 0;
        
        for (int i = 0; i < 20; i++) {
            st = new StringTokenizer(br.readLine());
            String major = st.nextToken();
            double credit = Double.parseDouble(st.nextToken());
            String grade = st.nextToken();
            
            if (grade.equals("P")) continue;
            
            double gradePoint = 0;
            switch (grade) {
                case "A+": gradePoint = 4.5; break;
                case "A0": gradePoint = 4.0; break;
                case "B+": gradePoint = 3.5; break;
                case "B0": gradePoint = 3.0; break; 
                case "C+": gradePoint = 2.5; break;
                case "C0": gradePoint = 2.0; break;
                case "D+": gradePoint = 1.5; break;
                case "D0": gradePoint = 1.0; break;
                case "F": gradePoint = 0.0; break;    
            }
            totalScore += credit * gradePoint;
            totalCredit += credit;
        }
        double majorGPA = totalScore / totalCredit;
        
        System.out.print(majorGPA);
    }
}

 

'코딩 테스트(Coding Test) > 백준' 카테고리의 다른 글

[JAVA] 백준 10단계  (0) 2024.11.12
[JAVA] 백준 7단계  (0) 2024.09.10
[JAVA] 백준 5단계  (3) 2024.09.09
[JAVA] 백준 4단계  (0) 2024.09.05
[JAVA] 백준 3단계  (0) 2024.09.04