매일 Algorithm

신재원·2023년 4월 8일
0

Algorithm

목록 보기
89/243

백준 11005번 (Bronze 1)

import java.util.Scanner;

public class problem284 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt(); // 몇 진법인지 입력
        StringBuilder sb = new StringBuilder();

        // 65 ~ 90 대문자 아스키코드
        while (n > 0) {
            // 나머지가 10 이상이면 A,B,C 아스키 코드로 표현
            if (n % m >= 10) {
                sb.append((char) ((n % m) + 55));
            } else {
                sb.append(n % m);
            }
            n /= m;
        }

        // 나머지를 append 함으로, reverse() 해줘야된다.
        System.out.print(sb.reverse().toString());
    }
}

백준 11179번 (Bronze 1)

import java.util.Scanner;

public class problem285 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int n = in.nextInt();

        // Integer.toBinaryString() : 2진수 문자열로 변환
        String temp = Integer.toBinaryString(n);
        sb.append(temp);
        sb.reverse();

        // 2진수 문자열을 10진수 정수로 변환
        System.out.print(Integer.parseInt(sb.toString(), 2));
    }
}

백준 11557번 (Bronze 1)

import java.util.Scanner;

public class problem286 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int t = in.nextInt();

        for (int i = 0; i < t; i++) {
            int school = in.nextInt();
            String[] name = new String[school];
            int[] n = new int[school];
            for (int j = 0; j < school; j++) {
                name[j] = in.next(); // 학교 입력
                n[j] = in.nextInt(); // 갯수 입력
            }
            int max = n[0]; // max값을 배열 처음의 값으로 할당
            int index = 0;
            for (int k = 0; k < n.length; k++) {
                if (max < n[k]) {
                    max = n[k]; // int배열 n을 탐색하여 max값 할당
                    index = k;
                }
            }

            System.out.println(name[index]);
        }
    }
}

백준 16171번 (Bronze 1)

import java.util.Scanner;

public class problem287 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String a = in.next();
        String b = in.next();

        String total = "";

        for (int i = 0; i < a.length(); i++) {

            // 정수 범위인지 검증
            if (!(a.charAt(i) - '0' >= 0 && a.charAt(i) - '0' <= 9)) {
                total += a.charAt(i);
            }
        }

        // total 문자열에 입력된 b 문자열이 contains(포함) 되어있는지 검증
        if (total.contains(b)) {
            System.out.print(1);
        } else {
            System.out.print(0);
        }
    }
}

백준 16968번 (Bronze 1)

import java.util.Scanner;

public class problem288 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        char[] ch = in.next().toCharArray();
        int result = 1;
        for (int i = 0; i < ch.length; i++) {

            // 첫번째 문자인 경우
            if (i == 0) {
                if (ch[i] == 'c') {
                    result *= 26;
                } else {
                    result *= 10;
                }
            } else {
                if (ch[i] == ch[i - 1]) {
                    if (ch[i] == 'c') {
                     // 한개의 문자를 동시에 사용하면 안돼서, 26 - 1
                        result *= 25;
                    } else {
                        result *= 9;
                    }
                } else {
                    if (ch[i] == 'c') {
                        result *= 26;
                    } else {
                        result *= 10;
                    }
                }
            }
        }
        System.out.print(result);
    }
}

0개의 댓글