문제: 정수가 담긴 리스트 num_list가 주어질 때, num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return 하도록 solution 함수를 완성해보세요.
내 풀이
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> num_list) {
vector<int> answer;
int odd_size = 0, even_size = 0;
for(int i = 0; i<num_list.size(); i++) {
if( (num_list[i]% 2) == 0) {
even_size++;
}
else {
odd_size++;
}
}
answer.push_back(even_size);
answer.push_back(odd_size);
return answer;
}
출처: https://school.programmers.co.kr/learn/courses/30/lessons/120824
'밥벌이 > 코딩테스트' 카테고리의 다른 글
프로그래머스 스택큐/ 주식가격 (0) | 2022.12.19 |
---|---|
프로그래머스 스택큐_배열에서 중복된 값 삭제 후 출력하기 (0) | 2022.12.16 |
프로그래머스 점의 위치 구하기 (7/100) (0) | 2022.12.12 |
프로그래머스 문자열 뒤집기(6/100) (0) | 2022.12.12 |
프로그래머스 배열 원소 길이(5/100) (0) | 2022.12.12 |