📌Back-End/프로그래머스

Java 프로그래머스 코딩 기초 트레이닝 Day 24 조건문 활용, 반복문 활용, 이차원 리스트(배열)

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

 

커피 심부름

프로그래머스 커피 심부름 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 24
 * 커피 심부름
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution(new String[]{"cafelatte", "americanoice", "hotcafelatte", "anything"}));
        System.out.println(solution(new String[]{"americanoice", "americano", "iceamericano"}));
    }

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

        for (String s : order) {
            if (s.contains("americano") || s.contains("anything")) {
                answer += 4500;
            } else {
                answer += 5000;
            }
        }

        return answer;
    }
}

 

 

그림 확대

프로그래머스 그림 확대 자바

 

 

import java.util.LinkedList;

/**
 * 코딩 기초 트레이닝 Day 24
 * 그림 확대
 */
public class Main2 {
    public static void main(String[] args) {
        String[] result = solution(
                new String[]{".xx...xx.", "x..x.x..x", "x...x...x", ".x.....x.", "..x...x..", "...x.x...", "....x...."},
                2
        );

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();

        result = solution(new String[]{"x.x", ".x.", "x.x"}, 3);

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();
    }

    public static String[] solution(String[] picture, int k) {
        LinkedList<String> list = new LinkedList<>();

        for (int i = 0; i < picture.length; i++) {
            StringBuilder sb = new StringBuilder();
            String[] strArr = picture[i].split("");

            for (String s : strArr) {
                sb.append(s.repeat(k));
            }

            for (int j = 0; j < k; j++) {
                list.add(sb.toString());
            }
        }

        return list.stream().toArray(String[]::new);
    }
}

 

 

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

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

 

 

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

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

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

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

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

        return answer;
    }
}

 

 

l로 만들기

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

 

 

/**
 * 코딩 기초 트레이닝 Day 24
 * l로 만들기
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution("abcdevwxyz"));
        System.out.println(solution("jjnnllkkmm"));
    }

    public static String solution(String myString) {
        StringBuilder sb = new StringBuilder();
        char[] charArr = myString.toCharArray();

        for (char c : charArr) {
            if (c < 'l') {
                sb.append('l');
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }
}

 

 

특별한 이차원 배열 1

프로그래머스 특별한 이차원 배열 1 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 24
 * 특별한 이차원 배열 1
 */
public class Main5 {
    public static void main(String[] args) {
        int[][] result = solution(3);

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

        result = solution(6);
        for (int[] arr : result) {
            for (int num : arr) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
        System.out.println();

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

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

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    answer[i][j] = 1;
                } else {
                    answer[i][j] = 0;
                }
            }
        }

        return answer;
    }
}

 

 

728x90
반응형