📌Back-End/프로그래머스

Java 프로그래머스 코딩 기초 트레이닝 Day 15 리스트(배열), 문자열

구 일 2024. 6. 11. 16:27
728x90
반응형

 

 

조건에 맞게 수열 변환하기1

프로그래머스 조건에 맞게 수열 변환하기 1 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 15
 * 조건에 맞게 수열 변환하기 1
 */
public class Main1 {
    public static void main(String[] args) {
        int[] result = solution(new int[]{1, 2, 3, 100, 99, 98});

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

    public static int[] solution(int[] arr) {
        int[] answer = new int[arr.length];

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 50 && arr[i] % 2 == 0) {
                answer[i] = arr[i] / 2;
            } else if (arr[i] < 50 && arr[i] % 2 != 0){
                answer[i] = arr[i] * 2;
            } else {
                answer[i] = arr[i];
            }
        }

        return answer;
    }
}

 

 

 

조건에 맞게 수열 변환하기 2

프로그래머스 조건에 맞게 수열 변환하기 2 자바

 

 

import java.util.Arrays;

/**
 * 코딩 기초 트레이닝 Day 15
 * 조건에 맞게 수열 변환하기 2
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 2, 3, 100, 99, 98}));
    }

    public static int solution(int[] arr) {
        int answer = 0;
        int[] arrX = new int[arr.length];
        boolean match = false;

        while (!match) {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] >= 50 && arr[i] % 2 == 0) {
                    arrX[i] = arr[i] / 2;
                } else if (arr[i] < 50 && arr[i] % 2 != 0) {
                    arrX[i] = arr[i] * 2 + 1;
                } else {
                    arrX[i] = arr[i];
                }
            }

            answer++;
            match = Arrays.equals(arr, arrX);

            if (!match) {
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = arrX[i];
                }
            }
        }

        return answer - 1;
    }
}

 

 

 

1로 만들기

프로그래머스 1로 만들기 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 15
 * 1로 만들기
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{12, 4, 15, 1, 14}));
    }

    public static int solution(int[] num_list) {
        int answer = 0;
        boolean flag = false;

        while (!flag) {
            for (int i = 0; i < num_list.length; i++) {
                if (num_list[i] != 1) {
                    if (num_list[i] % 2 == 0) {
                        num_list[i] = num_list[i] / 2;
                    } else {
                        num_list[i] = (num_list[i] - 1) / 2;
                    }

                    answer++;
                }
            }

            for (int i = 0; i < num_list.length; i++) {
                if (num_list[i] != 1) {
                    flag = false;
                    break;
                } else {
                    flag = true;
                }
            }
        }

        return answer;
    }
}

 

 

 

길이에 따른 연산

프로그래머스 길이에 따른 연산 자바

 

 

import java.util.Arrays;
import java.util.stream.IntStream;

/**
 * 코딩 기초 트레이닝 Day 15
 * 길이에 따른 연산
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{3, 4, 5, 2, 5, 4, 6, 7, 3, 7, 2, 2, 1}));
        System.out.println(solution(new int[]{2, 3, 4, 5}));
    }

    public static int solution(int[] num_list) {
        int answer = 0;

        if (num_list.length >= 11) {
            answer = Arrays.stream(num_list).sum();
        } else {
            answer = IntStream.of(num_list).reduce(1, (a, b) -> a * b);
        }

        return answer;
    }
}

 

 

 

원하는 문자열 찾기

프로그래머스 원하는 문자열 찾기 자바

 

/**
 * 코딩 기초 트레이닝 Day 15
 * 원하는 문자열 찾기
 */
public class Main5 {
    public static void main(String[] args) {
        System.out.println(solution("AbCdEfG", "aBc"));
        System.out.println(solution("aaAA", "aaaaa"));
    }

    public static int solution(String myString, String pat) {
        return myString.toLowerCase().contains(pat.toLowerCase()) ? 1 : 0;
    }
}

 

 

728x90
반응형