배열 뒤집기

김토리·2024년 2월 5일

알고리즘

목록 보기
12/27

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// num_list_len은 배열 num_list의 길이입니다.
int* solution(int num_list[], size_t num_list_len) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(num_list_len*4);
    int j=0;
    for(int i = num_list_len -1 ; i>=0 ; i--,j++){
        answer[j] = num_list[i];
    }
    return answer;
}

매개변수로 받은 배열 속 숫자들을 뒤집어서 새로운 배열에 넣는 것인데
새로운 배열 0부터 매개변수 배열은 사이즈-1부터 거꾸로 불러와 넣으면 된다.
"반복문의 응용"

int* answer = (int*)malloc(num_list_len*sizeof(int)); 
profile
웹 개발하며 데이터 분석, AI 공부하는 jinveloper

0개의 댓글