📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문

구 일 2024. 7. 18. 16:11
728x90
반응형

 

치킨 쿠폰

프로그래머스 치킨 쿠폰 자바

 

 

/**
 * 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문
 * 치킨 쿠폰
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution(100));
        System.out.println(solution(1081));
    }

    public static int solution(int chicken) {
        int answer = 0;

        while (chicken >= 10) {
            int tmp = chicken % 10;
            chicken /= 10;
            answer += chicken;
            chicken += tmp;
        }

        return answer;
    }
}

 

 

이진수 더하기

프로그래머스 이진수 더하기 자바

 

 

/**
 * 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문
 * 이진수 더하기
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution("10", "11"));
        System.out.println(solution("1001", "1111"));
    }

    public static String solution(String bin1, String bin2) {
        int sum = Integer.parseInt(bin1, 2) + Integer.parseInt(bin2, 2);
        return Integer.toBinaryString(sum);
    }
}

 

 

A로 B 만들기

프로그래머스 A로 B 만들기 자바

 

 

import java.util.Arrays;

/**
 * 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문
 * A로 B 만들기
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution("olleh", "hello"));
        System.out.println(solution("allpe", "apple"));
    }

    public static int solution(String before, String after) {
        char[] beforeArr = before.toCharArray();
        char[] afterArr = after.toCharArray();

        Arrays.sort(beforeArr);
        Arrays.sort(afterArr);

        return String.valueOf(beforeArr).equals(String.valueOf(afterArr)) ? 1 : 0;
    }
}

 

 

k의 개수

프로그래머스 k의 개수 자바

 

 

/**
 * 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문
 * k의 개수
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(1, 13, 1));
        System.out.println(solution(10, 50, 5));
        System.out.println(solution(3, 10, 2));
    }

    public static int solution(int i, int j, int k) {
        int answer = 0;

        for (int l = i; l <= j; l++) {
            String[] strArr = String.valueOf(l).split("");
            for (String s : strArr) {
                if (s.equals(String.valueOf(k))) {
                    answer++;
                }
            }
        }

        return answer;
    }
}

 

 

 

728x90
반응형