[프로그래머스 문제풀이] 1. 완주하지 못한 선수

WIGWAG·2022년 12월 31일
0

프로그래머스

목록 보기
1/32

※ participant 집합과 completion 집합의 차집합을 구하는 문제

표준라이브러리에서 제공하는 < algorithm >헤더의 집합 알고리즘 함수를 사용한다.

std::includes(처음1, 끝1, 처음2, 끝2, 비교, 프로젝션1, 프로젝션2)
std::set_difference(처음1, 끝1, 처음2, 끝2, 출력범위의 처음, 비교, 프로젝션1, 프로젝션2)
std::set_intersection(처음1, 끝1, 처음2, 끝2, 출력범위의 처음, 비교, 프로젝션1, 프로젝션2)
std::set_symmetric_difference(처음1, 끝1, 처음2, 끝2, 출력범위의 처음, 비교, 프로젝션1, 프로젝션2)
std::set_union(처음1, 끝1, 처음2, 끝2, 출력범위의 처음, 비교, 프로젝션1, 프로젝션2)

첫 번째는 집합 간 포함여부를 알려주는 함수,
두 번째는 차집합,
세 번째는 교집합,
네 번째는 xor,
다섯 번째는 합집합이다.

이 중 두 번째인 차집합 함수를 사용해서 문제를 푼다.

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    vector<string> answer;

    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
    set_difference(participant.begin(), participant.end(), completion.begin(), completion.end(), back_inserter(answer));

    return answer[0];
}

int main()
{
    cout << solution({ "leo", "kiki", "eden" }, { "eden", "kiki" }) << endl;
}

실행결과

leo


완주하지 못한 선수 문제 링크

profile
윅왁의 프로그래밍 개발노트

0개의 댓글