본문 바로가기

프로그래머스/lv0

배열 뒤집기

/* 배열 뒤집기
 * 정수가 들어 있는 배열 num_list가 매개변수로 주어집니다.
 * num_list의 원소의 순서를 거꾸로 뒤집은 배열을 return
 *
 * num_list                 result
 * [1, 2, 3, 4, 5]          [5, 4, 3, 2, 1]
 * [1, 1, 1, 1, 1, 2]       [2, 1, 1, 1, 1, 1]
 * [1, 0, 1, 1, 1, 3, 5]    [5, 3, 1, 1, 1, 0, 1]
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class programmer_0_51 {
    static int[] a1 = {12345};
    static int[] a2 = {111112};
    static int[] a3 = {1011135};
    public int[] solution(int[] num_list) {
        int[] answer = new int[num_list.length];
        int idx = 0;
        for(int i = num_list.length-1; i >= 0; i--){
            answer[idx] = num_list[i];
            idx++;
        }
        for(int i : answer) System.out.println(i);
        return answer;
    }
    public static void main(String args[]){
        programmer_0_51 t = new programmer_0_51();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
        System.out.println("result3 = " + t.solution(a3));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

옷가게 할인받기  (0) 2022.12.05
아이스 아메리카노  (0) 2022.12.05
짝수 홀수 개수  (0) 2022.12.05
배열 자르기  (0) 2022.12.05
이진수 더하기  (1) 2022.12.05