📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 5 수학, 배열

구 일 2024. 6. 26. 20:13
728x90
반응형

 

옷가게 할인 받기

프로그래머스 옷가게 할인 받기 자바

 

 

/**
 * 코딩테스트 입문 Day 5 수학, 배열
 * 옷가게 할인 받기
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution(150000));
        System.out.println(solution(580000));
    }

    public static int solution(int price) {
        if (price >= 500_000) {
            price = (int) (price * 0.8);
        } else if (price >= 300_000) {
            price = (int) (price * 0.9);
        } else if (price >= 100_000) {
            price = (int) (price * 0.95);
        }

        return price;
    }
}

 

 

아이스 아메리카노

프로그래머스 아이스 아메리카노 자바

 

 

/**
 * 코딩테스트 입문 Day 5 수학, 배열
 * 아이스 아메리카노
 */
public class Main2 {
    public static void main(String[] args) {
        int[] result = solution(5500);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

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

    public static int[] solution(int money) {
        int[] answer = new int[2];

        int price = 5500;
        int cnt = money / price;
        int changes = money - (price * cnt);

        answer[0] = cnt;
        answer[1] = changes;

        return answer;
    }
}

 

 

나이 출력

프로그래머스 나이 출력 자바

 

 

/**
 * 코딩테스트 입문 Day 5 수학, 배열
 * 나이 출력
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(40));
        System.out.println(solution(23));
    }

    public static int solution(int age) {
        int year = 2022;
        return year - age + 1;
    }
}

 

 

배열 뒤집기

프로그래머스 배열 뒤집기 자바

 

 

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 코딩테스트 입문 Day 5 수학, 배열
 * 배열 뒤집기
 */
public class Main4 {
    public static void main(String[] args) {
        int[] result = solution(new int[]{1, 2, 3, 4, 5});
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(new int[]{1, 1, 1, 1, 1, 2});
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(new int[]{1, 0, 1, 1, 1, 3, 5});
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static int[] solution(int[] num_list) {
        List<Integer> list =  Arrays.stream(num_list).boxed().collect(Collectors.toList());
        Collections.reverse(list);

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

728x90
반응형