📌Back-End/프로그래머스

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

구 일 2024. 7. 19. 22:07
728x90
반응형

 

문자열 밀기

프로그래머스 문자열 밀기 자바

 

 

/**
 * 코딩테스트 입문 Day 25 시뮬레이션, 조건문, 수학
 * 문자열 밀기
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("hello", "ohell"));
        System.out.println(solution("apple", "elppa"));
        System.out.println(solution("atat", "tata"));
        System.out.println(solution("abc", "abc"));
    }

    public static int solution(String A, String B) {
        StringBuilder sb = new StringBuilder(A);
        int len = A.length();
        int answer = 0;

        for (int i = 0; i < len; i++) {
            if (sb.toString().equals(B)) {
                return answer;
            }

            char tmp = A.charAt(len - i - 1);
            sb.deleteCharAt(len - 1);
            sb.insert(0, tmp);
            answer++;
        }

        return -1;
    }
}

 

 

종이 자르기

프로그래머스 종이 자르기 자바

 

 

/**
 * 코딩테스트 입문 Day 25 시뮬레이션, 조건문, 수학
 * 종이 자르기
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(2, 2));
        System.out.println(solution(2, 5));
        System.out.println(solution(1, 1));
    }

    public static int solution(int M, int N) {
        return M * N - 1;
    }
}

 

 

연속된 수의 합

프로그래머스 연속된 수의 합 자바

 

 

/**
 * 코딩테스트 입문 Day 25 시뮬레이션, 조건문, 수학
 * 연속된 수의 합
 */
public class Main3 {
    public static void main(String[] args) {
        int[] result = solution(3, 12);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(5, 15);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(4, 14);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(5, 5);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static int[] solution(int num, int total) {
        int[] answer = new int[num];

        for (int i = 0; i < num; i++) {
            total -= i;
        }

        int tmp = total / num;

        for (int i = 0; i < num; i++) {
            answer[i] = tmp + i;
        }

        return answer;
    }
}

 

 

다음에 올 숫자

프로그래머스 다음에 올 숫자 자바

 

 

/**
 * 코딩테스트 입문 Day 25 시뮬레이션, 조건문, 수학
 * 다음에 올 숫자
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 2, 3, 4}));
        System.out.println(solution(new int[]{2, 4, 8}));
    }

    public static int solution(int[] common) {
        int answer = 0;
        int diff = 0;
        int lastNum = common[common.length - 1];

        if (common[1] - common[0] == common[2] - common[1]) {
            diff = common[1] - common[0];
            answer = lastNum + diff;
        } else {
            diff = common[1] / common[0];
            answer = lastNum * diff;
        }

        return answer;
    }
}

 

 

 

728x90
반응형