본문 바로가기
CodingTest

코딩테스트 입문 - Day 9

by Jiwon_Loopy 2025. 4. 20.
반응형

개미 군단


class Solution {
    public int solution(int hp) {
        int sum = 0;
        for(int i = 5; i > 0; i -= 2){
            sum += hp/i;
            hp %= i; 
        }
        return sum;
    }
}

모스 부호 (1)


import java.util.HashMap;

class Solution {
	static String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
			"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };

	
	public static void main(String[] args) {
		System.out.println(solution(".... . .-.. .-.. ---"));
	}
	
	public static String solution(String letter) {
        StringBuilder sb = new StringBuilder();
        HashMap<String, Character> hm = new HashMap<String, Character>();
        int ch = 0;
        for(String m : morse){
            hm.put(m ,(char) (97 + ch++));
        }
        String sp[] = letter.split(" ");
        for(String s : sp){
            sb.append(hm.get(s));
        }
        return sb.toString();
	}
}

가위 바위 보


class Solution {
    public String solution(String rsp) {
        StringBuilder sb = new StringBuilder();
        for(int i =0; i<rsp.length();i++){
            char ch = ' ';
            char select = rsp.charAt(i);
            if(select == '0'){
                ch = '5';
            }
            else if(select == '2'){
                ch = '0';
            }
            else{
                ch = '2';
            }
            sb.append(ch);
        }
        return sb.toString();
    }
}

구슬을 나누는 경우의 수


class Solution {
    public long solution(int balls, int share) {
        share = Math.min(share, balls - share);
        long mul = 1L;
        long div = 1L;
        for(int i = 0; i < share; i++){
            mul *= (balls - i);
            div *= (i + 1);
            if(mul % div == 0){
                mul = mul/div;
                div = 1;
            }
        }
        return mul/div;
    }
}
728x90
반응형

'CodingTest' 카테고리의 다른 글

코딩테스트 입문 - Day 11  (0) 2025.04.20
코딩테스트 입문 - Day 10  (0) 2025.04.20
프로그래머스 - 특수문자 출력하기  (1) 2025.04.12
코딩테스트 입문 - Day 8  (0) 2025.04.12
코딩테스트 입문 - Day 7  (0) 2025.04.12