문제
정수가 담긴 리스트 num_list가 주어질 때, num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return 하도록 solution 함수를 완성해보세요.
솔루션
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> num_list) {
vector<int> answer;
int cnt_odd = 0;
int cnt_even = 0;
for(int i = 0; i < num_list.size(); i++){
if(num_list[i]%2==0){
cnt_even++;
}
else{
cnt_odd++;
}
}
answer.push_back(cnt_even);
answer.push_back(cnt_odd);
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/120824
'프로그래머스코딩테스트연습풀이 > C++' 카테고리의 다른 글
[프로그래머스/C++] 문자열 뒤집기 (0) | 2025.02.12 |
---|---|
[프로그래머스/C++] 두 수의 합 (0) | 2025.02.12 |
[프로그래머스/C++] 배열 자르기 (0) | 2025.02.12 |
[프로그래머스/C++] 문자 반복 출력하기 (0) | 2025.02.12 |
[프로그래머스/C++] 순서쌍의 개수 (0) | 2025.02.11 |