📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 9 수학, 문자열, 해시, 완전탐색, 조건문

구 일 2024. 7. 1. 15:59
728x90
반응형

 

개미 군단

프로그래머스 개미 군단 자바

 

 

/**
 * 코딩테스트 입문 Day 9 수학, 문자열, 해시, 완전탐색, 조건문
 * 개미 군단
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution(23));
        System.out.println(solution(24));
        System.out.println(solution(999));
    }

    public static int solution(int hp) {
        int answer = 0;
        int general = 5;
        int soldier = 3;
        int worker = 1;

        while (hp > 0) {
            if (hp >= general) {
                hp -= general;
                answer++;
            } else if (hp >= soldier) {
                hp -= soldier;
                answer++;
            } else {
                hp -= worker;
                answer++;
            }
        }

        return answer;
    }
}

 

 

모스부호 (1)

프로그래머스 모스부호 (1) 자바

 

 

import java.util.HashMap;

/**
 * 코딩테스트 입문 Day 9 수학, 문자열, 해시, 완전탐색, 조건문
 * 모스부호 (1)
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(".... . .-.. .-.. ---"));
        System.out.println(solution(".--. -.-- - .... --- -."));
    }

    public static String solution(String letter) {
        HashMap<String, Character> hm = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        String[] morse = {
                ".-","-...","-.-.","-..",".","..-.","--.","....","..",
                ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
                "...","-","..-","...-",".--","-..-","-.--","--.."
        };
        char alphabet = 'a';

        for (String s : morse) {
            hm.put(s, alphabet++);
        }

        String[] letterArr = letter.split(" ");
        for (String s : letterArr) {
            sb.append(hm.get(s));
        }

        return sb.toString();
    }
}

 

 

가위 바위 보

프로그래머스 가위 바위 보 자바

 

 

import java.util.HashMap;

/**
 * 코딩테스트 입문 Day 9 수학, 문자열, 해시, 완전탐색, 조건문
 * 가위 바위 보
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution("2"));
        System.out.println(solution("205"));
    }

    public static String solution(String rsp) {
        StringBuilder sb = new StringBuilder();
        HashMap<String, String> hm = new HashMap<>();
        hm.put("2", "0");
        hm.put("0", "5");
        hm.put("5", "2");

        String[] rspArr = rsp.split("");
        for (String s: rspArr) {
            sb.append(hm.get(s));
        }
        return sb.toString();
    }
}

 

 

구슬을 나누는 경우의 수

프로그래머스 구슬을 나누는 경우의 수 자바

 

 

/**
 * 코딩테스트 입문 Day 9 수학, 문자열, 해시, 완전탐색, 조건문
 * 구슬을 나누는 경우의 수
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(3, 2));
        System.out.println(solution(5, 3));
    }

    public static int solution(int balls, int share) {
        return combination(balls, share);
    }

    private static int combination(int balls, int share) {
        if (balls == share || share == 0) {
            return 1;
        }

        return combination((balls - 1), (share)) + combination(balls - 1, share - 1);
    }
}

 

 

728x90
반응형