티스토리 뷰
문제 출처 - https://programmers.co.kr/learn/courses/30/lessons/12910
코딩테스트 연습 - 나누어 떨어지는 숫자 배열 | 프로그래머스
array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요. divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하세요. 제한사항 arr은 자연수를 담은 배열입니다. 정수 i, j에 대해 i ≠ j 이면 arr[i] ≠ arr[j] 입니다. divisor는 자연수입니다. array는 길이 1 이상인 배열입니다. 입출력 예 arr divi
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr, int divisor) {
vector<int> answer;
for(int i=0; i<arr.size(); i++) {
if(arr[i] % divisor == 0)
answer.push_back(arr[i]);
}
if(answer.empty())
answer.push_back(-1);
sort(answer.begin(), answer.end());
return answer;
}
'ALGORITHM > 프로그래머스' 카테고리의 다른 글
[C++]프로그래머스 - 디스크 컨트롤러(level3) (0) | 2019.11.30 |
---|---|
[C++]프로그래머스 - 큰 수 만들기(level2) (0) | 2019.11.30 |
[C++]프로그래머스 - 도둑질(level4) (0) | 2019.11.22 |
[C++]프로그래머스 - 네트워크(level3) (0) | 2019.11.22 |
[C++]프로그래머스 - 탑(level2) (0) | 2019.11.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 문자열
- left join
- C++
- 순열
- 2020 KAKAO BLIND RECRUITMENT
- 우선순위큐
- 2019 Kakao Blind Recruitment
- 파이썬
- 해시
- 코딩테스트
- dictionary
- 구현
- SWExpert
- BOJ
- SW Expert
- 완전탐색
- 힙
- 스택
- 괄호
- 프로그래머스
- 문자열처리
- 정렬
- 백준
- Permutation
- combination
- 재귀
- programmers
- 딕셔너리
- Python
- hash
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함