[C++] 프로그래머스 n 번째 원소까지

문제설명

정수 리스트 num_list와 정수 n이 주어질 때,
num_list의 첫 번째 원소부터 n 번째 원소까지의
모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.


제한사항

• 2 ≤ num_list의 길이 ≤ 30
• 1 ≤ num_list의 원소 ≤ 9
• 1 ≤ n ≤ num_list의 길이


입출력 예

num_list n result
[2, 1, 6] 1 [2]
[5, 2, 1, 7, 5] 3 [5, 2, 1]


풀이

n 번째 원소까지 return하는 간단한 구현 문제입니다.

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list, int n) {
    vector<int> answer;
    
    for(int i = 0; i < n; i++)
    {
        answer.push_back(num_list[i]);
    }
    
    return answer;
}


결과

코드 실행결과

Categories:

Updated:

Leave a comment