매일 Algorithm

신재원·2023년 4월 17일
0

Algorithm

목록 보기
99/243

백준 1864번 (Bronze 2)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

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


        Map<String, Integer> map = new HashMap<>();

        // map 객체에 값 보관
        map.put("-", 0);
        map.put("\\", 1);
        map.put("(", 2);
        map.put("@", 3);
        map.put("?", 4);
        map.put(">", 5);
        map.put("&", 6);
        map.put("%", 7);
        map.put("/", -1);

        // 반복문
        while (true) {
            String str = in.next();

            // 탈출 조건
            if (str.equals("#")) {
                break;
            }

            // map 객체와 비교를 위해 split
            String[] s = str.split("");

            int result = 0;

            for (int i = 0; i < s.length; i++) {
                // 자릿수의 제곱
                // 문자배열 s의 해당되는 value값을 가져온다
                int temp = map.get(s[i]);

                // pow(사용할 진수값, 지수값), ex pow(8,2) 8의 2제곱
                result += temp * Math.pow(8, s.length - i - 1);
            }
            System.out.println(result);
        }

    }
}

백준 1871번 (Bronze 2)

import java.util.Scanner;

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


        int t = in.nextInt();


        for (int i = 0; i < t; i++) {
            String str = in.next();
            String[] arr = str.split("-");
            String a = arr[0]; // 세 글자
            String b = arr[1]; // 네 숫자

            int totalA = 0;
            for (int j = 0; j < a.length(); j++) {
                int temp = a.charAt(j) - 'A';
                // 자릿수의 제곱값 계산
                temp *= Math.pow(26, 2 - j);
                totalA += temp;
            }

            int totalB = Integer.parseInt(b);

            // 절대값이 100 이하면 nice, 그렇지 않으면 not nice
            if (Math.abs(totalA - totalB) <= 100) {
                System.out.println("nice");
            } else {
                System.out.println("not nice");
            }

        }
    }
}

백준 1964번 (Bronze 2)

import java.util.Scanner;

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

        int n = in.nextInt();
        int result = 5;


        for (int i = 1; i < n; i++) {
            // ex : 입력값 3
            // 5 + (3*3-2) + (4*3-2) 이다.
            result = (result + (i + 2) * 3 - 2) % 45678;
        }
        System.out.println(result);
    }
}

백준 2145번 (Bronze 2)

import java.util.Scanner;

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

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

            // 탈출 조건
            if (n == 0) {
                break;
            }
            while (n >= 10) {
                int sum = 0;
                while (n > 0) {
                    // 누적합 갱신
                    sum += n % 10;
                    n /= 10;
                }
                n = sum;
            }
            System.out.println(n);
        }
    }
}

0개의 댓글