매일 Algorithm

신재원·2023년 4월 6일
0

Algorithm

목록 보기
88/243

백준 6996번 (Bronze 1)

import java.util.Arrays;
import java.util.Scanner;

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

        int size = in.nextInt();

        for (int i = 0; i < size; i++) {
            String a = in.next(); // 문자열 a 입력
            String b = in.next(); // 문자열 b 입력

            // char배열로 변환
            char[] c = a.toCharArray();
            char[] d = b.toCharArray();
            Arrays.sort(c);
            Arrays.sort(d);

            if (Arrays.equals(c, d)) {
                System.out.println(a + " & " + b + " are anagrams.");
            } else {
                System.out.println(a + " & " + b + " are NOT anagrams.");
            }
        }
    }
}

백준 9506번 (Bronze 1)

import java.util.Scanner;

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

        StringBuilder sb = new StringBuilder();
        while (true) {
            int n = in.nextInt();

            // 탈출조건
            if (n == -1) {
                break;
            }

            // 약수의 누적 합 구하기
            int sum = 0;
            for (int i = 1; i < n; i++) {
                if (n % i == 0) {
                    sum += i;
                }
            }

            // 약수의 누적합과 입력값이 같을경우
            if (sum == n) {
                sb.append(n + " = 1");

                for (int i = 2; i < n; i++) {
                    if (n % i == 0) {
                        sb.append(" + ").append(i);
                    }
                }
                sb.append("\n");

            }
            // 약수의 누적합과 입력값이 다를경우
            else {
                sb.append(n);
                sb.append(" is NOT perfect. \n");
            }
        }
        System.out.print(sb.toString());

    }
}

백준 11170번 (Bronze 1)

import java.util.Scanner;

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

        int t = in.nextInt();

        for (int i = 0; i < t; i++) {
            int count = 0;
            int n = in.nextInt();
            int m = in.nextInt();

            for (int j = n; j <= m; j++) {
                int temp = j;
                if (temp == 0) {
                    count++;
                }
                while (temp > 1) {
                    // 100이 입력값이면 0의 갯수를 2개로 count
                    if (temp % 10 == 0) {
                        count++;
                    }
                    temp /= 10;
                }
            }
            System.out.println(count);
        }
    }
}

백준 12605번 (Bronze 1)

import java.util.Scanner;

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

        int t = in.nextInt();
        in.nextLine(); // 개행 제거
        int index = 1;
        for (int i = 0; i < t; i++) {
            String input = in.nextLine();
            String[] str = input.split(" ");
            System.out.print("Case #" + index + ": ");

            // str 배열을 뒤집어 뒤의 값 부터 꺼내기
            for (int j = str.length - 1; j >= 0; j--) {
                System.out.print(str[j] + " ");

            }

            System.out.println();
            index++;
        }
    }
}

백준 14405번 (Bronze 1)

import java.util.Scanner;

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

        String str = in.next();
        String[] pika = {"pi", "ka", "chu"};

        for (int i = 0; i < 3; i++) {
            str = str.replaceAll(pika[i], "0");
        }

        str = str.replace("0", "");
        if (str.length() == 0) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

0개의 댓글