📌Back-End/프로그래머스

Java 프로그래머스 코딩 기초 트레이닝 Day 23 조건문 활용

구 일 2024. 6. 19. 09:33
728x90
반응형

 

부분 문자열

프로그래머스 부분 문자열 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 23
 * 부분 문자열
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("abc", "aabcc"));
        System.out.println(solution("tbt", "tbbttb"));
    }

    public static int solution(String str1, String str2) {
        return str2.contains(str1) ? 1 : 0;
    }
}

 

 

꼬리 문자열

프로그래머스 꼬리 문자열 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 23
 * 꼬리 문자열
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(new String[]{"abc", "def", "ghi"}, "ef"));
        System.out.println(solution(new String[]{"abc", "bbc", "cbc"}, "c"));
    }

    public static String solution(String[] str_list, String ex) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < str_list.length; i++) {
            if (!str_list[i].contains(ex)) {
                sb.append(str_list[i]);
            }
        }

        return sb.toString();
    }
}

 

 

정수 찾기

프로그래머스 정수 찾기 자바

 

 

import java.util.Arrays;

/**
 * 코딩 기초 트레이닝 Day 23
 * 정수 찾기
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 2, 3, 4, 5}, 3));
        System.out.println(solution(new int[]{15, 98, 23, 2, 15}, 20));
    }

    public static int solution(int[] num_list, int n) {
        return Arrays.stream(num_list).anyMatch(x -> x == n) ? 1 : 0;
    }
}

 

 

주사위 게임 1

프로그래머스 주사위 게임 1 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 23
 * 주사위 게임 1
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(3, 5));
        System.out.println(solution(6, 1));
        System.out.println(solution(2, 4));
    }

    public static int solution(int a, int b) {
        int answer = 0;

        if (a % 2 != 0 && b % 2 != 0) {
            answer = (int) Math.pow(a, 2) + (int) Math.pow(b, 2);
        } else if (a % 2 == 0 && b % 2 == 0) {
            answer = Math.abs(a - b);
        } else {
            answer = 2 * (a + b);
        }

        return answer;
    }
}

 

 

날짜 비교하기

프로그래머스 날짜 비교하기 자바

 

 

import java.time.LocalDate;

/**
 * 코딩 기초 트레이닝 Day 23
 * 날짜 비교하기
 */
public class Main5 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{2021, 12, 28}, new int[]{2021, 12, 29}));
        System.out.println(solution(new int[]{1024, 10, 24}, new int[]{1024, 10, 24}));
    }

    public static int solution(int[] date1, int[] date2) {
        LocalDate localDate1 = LocalDate.of(date1[0], date1[1], date1[2]);
        LocalDate localDate2 = LocalDate.of(date2[0], date2[1], date2[2]);

        return localDate1.isBefore(localDate2) ? 1 : 0;
    }
}

 

 

728x90
반응형