https://school.programmers.co.kr/learn/courses/30/lessons/68644
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> numbers) {
vector<int> answer;
map<int,bool> m;
for(int i=0;i<numbers.size()-1;i++)
{
for(int j=i+1;j<numbers.size();j++)
{
if(m.find(numbers[i]+numbers[j])==m.end())
{
answer.push_back(numbers[i]+numbers[j]);
m.insert({numbers[i]+numbers[j],true});
}
}
}
sort(answer.begin(),answer.begin()+answer.size());
return answer;
}
주어진 정수 배열에 대해서 두 수를 뽑아서 더해 만들 수 있는 모든 수를 answer에 넣고 반환하느 문제이다.
따라서 이중반복문을 사용하며 각각의 수는 맵을 통해 중복확인을 하였다.
마지막으로 오름차순정렬후 리턴한다.
'알고리즘 > 프로그래머스 1단계' 카테고리의 다른 글
프로그래머스 - 푸드 파이트 대회 - C++ (0) | 2024.07.16 |
---|---|
프로그래머스 - 숫자 문자열과 영단어 - C++ (0) | 2024.07.16 |
프로그래머스 - 가장 가까운 글자 - C++ (0) | 2024.07.11 |
프로그래머스 - 최대공약수와 최소공배수 - C++ (0) | 2024.07.11 |
프로그래머스 - 최소 직사각형 - C++ (0) | 2024.07.10 |