본문 바로가기
[프로그래머스 C++]/LEVEL 1

[프로그래머스 C++] 두 개 뽑아서 더하기

by AKI(JUNI) 2025. 3. 31.

◈ 문제 설명


◈ 문제 설명 링크

코딩테스트 연습 - 두 개 뽑아서 더하기 | 프로그래머스 스쿨

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


◈ 작성 코드

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

using namespace std;

vector<int> solution(vector<int> numbers) 
{
    vector<int> answer;
    vector<int> ace;
    sort(numbers.begin(),numbers.end());
    
    for(int i = 0; i < numbers.size() - 1; i++)
    {
        for(int j = i + 1; j < numbers.size(); j++)
        {
            int a = numbers[i] + numbers[j];
            ace.push_back(a);
        }
    }
    sort(ace.begin(), ace.end());
    
    int t = 0;
    int c = -1;
    while(t < ace.size())
    {
        if(ace[t] != c)
        {
            answer.push_back(ace[t]);
            c = ace[t];
        }
        t++;
    }
    
    return answer;
}