https://school.programmers.co.kr/learn/courses/30/lessons/12915
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int wn;
bool compare(string a, string b)
{
if(a[wn]==b[wn])return a<b;
else if(a[wn]<b[wn])return true;
else return false;
}
vector<string> solution(vector<string> strings, int n) {
vector<string> answer;
wn=n;
sort(strings.begin(),strings.begin()+strings.size(),compare);
return strings;
}
주어진 벡터의 문자열들을 n의 인덱스를 기준으로 정렬하는 문제이다.
sort 함수에 사용자정의함수를 비교함수로 넣어서 해결하였다.
n값을 compare함수에 전하기 위해서 전역변수를 만들었다.
'알고리즘 > 프로그래머스 1단계' 카테고리의 다른 글
프로그래머스 - 콜라 문제 - C++ (0) | 2024.07.17 |
---|---|
프로그래머스 - 비밀지도 - C++ (0) | 2024.07.16 |
프로그래머스 - 푸드 파이트 대회 - C++ (0) | 2024.07.16 |
프로그래머스 - 숫자 문자열과 영단어 - C++ (0) | 2024.07.16 |
프로그래머스 - 두 개 뽑아서 더하기 - C++ (0) | 2024.07.16 |