본문 바로가기
IT

[JAVA] 백준 3003, 2941

by %? 2025. 4. 24.

[백준 3003]

 

import java.util.Scanner;

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

        int king = input.nextInt();
        int queen = input.nextInt();
        int rook = input.nextInt();
        int bishop = input.nextInt();
        int knight = input.nextInt();
        int pawn = input.nextInt();

        if (king >= 0 && king <= 10 && queen >= 0 && queen <= 10 && rook >= 0 && rook <= 10 && bishop >= 0 && bishop <= 10 && knight >= 0 && knight <= 10 && pawn >= 0 && pawn <= 10) {
            if (king == 1) {
                System.out.print("0 ");
            } else {
                System.out.print(1 - king + " ");
            }

            if (queen == 1) {
                System.out.print("0 ");
            } else {
                System.out.print(1 - queen + " ");
            }

            if (rook == 2) {
                System.out.print("0 ");
            } else {
                System.out.print(2 - rook + " ");
            }

            if (bishop == 2) {
                System.out.print("0 ");
            } else {
                System.out.print(2 - bishop + " ");
            }

            if (knight == 2) {
                System.out.print("0 ");
            } else {
                System.out.print(2 - knight + " ");
            }

            if (pawn == 8) {
                System.out.print("0");
            } else {
                System.out.print(8 - pawn);
            }
        }

        input.close();
    }
}

 

[백준 2941]

 

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String croatia[] = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};

        String str = input.nextLine();
        int count = 0;
        boolean find = false;

        if (str.length() <= 100) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (Character.isLowerCase(c) || c == '=' || c == '-') {
                    if (i + 2 < str.length()) {
                        for (int j = 0; j < croatia.length; j++) {
                            if (str.substring(i, i + 3).equals(croatia[j])) {
                                count++;
                                i += 2;
                                find = true;
                                break;
                            }
                        }
                    }
                    
                    if (i + 1 < str.length()) {
                        for (int j = 0; j < croatia.length; j++) {
                            if (str.substring(i, i + 2).equals(croatia[j])) {
                                count++;
                                i += 1;
                                find = true;
                                break;
                            }
                        }
                    }
                    
                    if (!find) {
                        count++;
                    } else {
                        find = false;
                    }
                }
            }
            System.out.println(count);
        }

        input.close();
    }
}

 


 

 개인적으로 2941번은 자바로 코드가 400B가 안 되길래 봤는데 10줄 정도로 해결한 걸 보고 놀랬습니다.. String에 대해서 잘 알면 되게 간단하게 해결 할 수 있는 문제였더라고요.

- 7387714 님의 코드입니다. -

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        String reg = "c=|c-|dz=|d-|lj|nj|s=|z=";
        String result = s.replaceAll(reg, " ");
        System.out.println(result.length());
    }
}