[C++] 프로그래머스 정수 찾기

문제설명

정수 리스트 num_list와 찾으려는 정수 n이 주어질 때,
num_list안에 n이 있으면 1을 없으면 0을
return하도록 solution 함수를 완성해주세요.


제한사항

• 3 ≤ num_list의 길이 ≤ 100
• 1 ≤ num_list의 원소 ≤ 100
• 1 ≤ n ≤ 100


입출력 예

num_list n result
[1, 2, 3, 4, 5] 3 1
[15, 98, 23, 2, 15] 20 0


풀이

num_list 안에 n이 있는지 파악하는 간단한 구현 문제입니다.

#include <string>
#include <vector>

using namespace std;

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


결과

코드 실행결과

Categories:

Updated:

Leave a comment