https://school.programmers.co.kr/learn/courses/30/lessons/147354
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
int answer = 0;
sort(data.begin(),data.end(),
[col](const vector<int>& a,const vector<int>&b){
if(a[col-1]!=b[col-1])
return a[col-1]<b[col-1];
else
return a[0]>b[0];
});
for(int i=row_begin;i<=row_end;i++)
{
int sum=0;
for(int item : data[i-1])
sum+=item%i;
if(i==row_begin)
answer = sum;
else if(i!=row_begin)
answer = sum^answer;
}
return answer;
}
주어진 data를 조건에 맞게 정렬하고 S_i 를 각각 구해서 answer XOR 연산으로 누적한 결과를 리턴하는 문제이다.
정렬은 sort함수로 사용했다. 이 때, 람다함수로 compare함수를 넘겨줬다.
col을 사용해야하기 때문에 [] 안에 col을 넣고 () 에 매개변수를 넣는다.
그리고 for문을 돌며 합계를 구하고 answer에 xor연산을 한다.
'알고리즘 > 프로그래머스 2단계' 카테고리의 다른 글
프로그래머스 - [3차] n진수 - C++ (0) | 2024.08.08 |
---|---|
프로그래머스 - 오픈채팅방 - C++ (0) | 2024.08.07 |
프로그래머스 - 튜플 - C++ (0) | 2024.08.07 |
프로그래머스 - [3차] 압축 - C++ (0) | 2024.08.06 |
프로그래머스 - 행렬의 곱셈 - C++ (0) | 2024.08.06 |