https://programmers.co.kr/learn/courses/30/lessons/12917

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <algorithm>
#include <ctype.h>
 
using namespace std;
 
bool mysort(char a, char b) {
    if (isupper(a) && islower(b)) {
        return false;
    }
    return a > b;
}
 
string solution(string s) {
    string answer = "";
 
    sort(s.begin(), s.end(), mysort);
    answer = s;
 
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

문자열 내의 문자 원소에 대해 비교함수 mysort를 정의해 sort()에 대입해주었다

 

반응형

https://programmers.co.kr/learn/courses/30/lessons/12915

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제에 제시된 제한 사항 
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int N;
 
bool compare_c(string a, string b) {
    if (a.at(N) != b.at(N)) return a.at(N) < b.at(N);
    else return a < b;
}
 
vector<string> solution(vector<string> strings, int n) {
    vector<string> answer;
 
    N = n;
 
    sort(strings.begin(), strings.end(), compare_c);
    answer = strings;
 
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

algorithm 라이브러리의 sort() 함수를 활용해 풀 수 있었다.

sort() 다양한 사용법 중 3번째 인자로 사용자가 정의한 비교함수를 넣어 사용할 수 있는 것을 알면 쉽게 풀 수 있음

n번째 인덱스 원소의 사전적 순서로 비교하는 사용자 정의 함수 compare_c 를 정의하여 sort()에 넣어주었다

 

sort() → algorithm 라이브러리에 정의된 함수로 기본적으로 Quick sort 알고리즘을 사용한다

  1. template <class RandomIt>

     sort(RandomIt begin, RandomIt end)

     → begin에서부터 end까지의 반복자 범위 내 원소들을 오름차순 정렬해준다.

         기본적으로 operator< 를 이용해 원소들을 비교

 

  2. template <class RandomIt, class Compare>

     sort(RandomIt begin, RandomIt end, Compare cmp)

     → begin에서부터 end까지의 반복자 범위 내 원소들을 오름차순 정렬해준다.

         cmp 비교함수를 사용해 비교하여 원소들을 비교 (사용자 정의 함수도 사용 가능)

         비교함수의 반환값은 boolean 타입으로 true일 경우 앞의 원소가 먼저 정렬된다

 

  * sort()의 사용법은 몇 개 더 있지만 두 가지만 정리해 두었습니다.

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

서울에서 김서방 찾기  (0) 2020.04.01
문자열 내림차순으로 배치하기  (0) 2020.04.01
나누어 떨어지는 숫자 배열  (0) 2020.03.30
가운데 글자 가져오기  (0) 2020.03.30
K번째수  (0) 2020.03.30

https://programmers.co.kr/learn/courses/30/lessons/12910

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;
    int arr_sz = arr.size();
    for (int i = 0; i < arr_sz; i++) {
        if (arr[i] % divisor == 0) {
            answer.push_back(arr[i]);
        }
    }
    if (answer.size() == 0) {
        answer.push_back(-1);
        return answer;
    }
    sort(answer.begin(), answer.end());
    
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

문자열 내림차순으로 배치하기  (0) 2020.04.01
문자열 내 마음대로 정렬하기  (0) 2020.04.01
가운데 글자 가져오기  (0) 2020.03.30
K번째수  (0) 2020.03.30
모의고사  (0) 2020.03.30

https://programmers.co.kr/learn/courses/30/lessons/12903

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) {
    string answer = "";
    int s_len = s.length();
    if (s_len % 2 == 0) {
        answer.assign(s.begin() + s_len / 2 - 1, s.begin() + s_len / 2 + 1);
    }
    else {
        answer = s[s_len / 2];
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

문자열 내 마음대로 정렬하기  (0) 2020.04.01
나누어 떨어지는 숫자 배열  (0) 2020.03.30
K번째수  (0) 2020.03.30
모의고사  (0) 2020.03.30
완주하지 못한 선수  (0) 2020.03.30

https://programmers.co.kr/learn/courses/30/lessons/42748

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    vector<int> temp;
    int commands_sz = commands.size();
    int i, j, k;
    //commands 2차원 배열에서 1개의 배열씩 추출해 arguments로 사용
    for (int a = 0; a < commands_sz; a++) {
        //2차원 배열 commands에서 추출된 i,j,k 
        i = commands[a][0];
        j = commands[a][1];
        k = commands[a][2];
 
        temp.assign(array.begin()+i-1, array.begin()+j);  //array의 i번쨰 ~j번째까지 복사된 vector 
        sort(temp.begin(), temp.end());  //array를 i~j범위로 복사한 temp vector를 sorting
        answer.push_back(temp[k - 1]); //i~j범위로 추출된 vector에서 추출한 k번째 값을 answer에 push
    }
 
    return answer;
 
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

1. commands의 값들을 i, j, k 로 순서대로 대입하여 assign() 함수를 활용해 임시 vector인 temp를 만든다.

2. sort() 를 통해 정렬을 거쳐 k번째수를 뽑아낸다.

3. 위의 과정을 commands 크기만큼 반복해주어 answer에 넣은 후 반환 

 

  *  assign()

     template<class InputIterator>

     void assign( InputIterator first, InputIterator last)

     → first 부터 last 까지의 원소들의 내용을 벡터에 저장

 

     template<typename T>   

     void assign(size_type n, const T& u);

     → 원래 내용을 다 지우고 원소 u를 n개 가지는 벡터

 

   

     
  

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

문자열 내 마음대로 정렬하기  (0) 2020.04.01
나누어 떨어지는 숫자 배열  (0) 2020.03.30
가운데 글자 가져오기  (0) 2020.03.30
모의고사  (0) 2020.03.30
완주하지 못한 선수  (0) 2020.03.30

https://programmers.co.kr/learn/courses/30/lessons/42840

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> answers) {
    vector<int> answer;
    int answers_sz = answers.size();
    int max;
    vector<int> p1 = { 1,2,3,4,5 };
    vector<int> p2 = { 2,1,2,3,2,4,2,5 };
    vector<int> p3 = { 3,3,1,1,2,2,4,4,5,5 };
 
    vector<int> p_num(3,0);
    //반복문을 통해 수포자 3명 패턴에 따른 개별 채점
    for (int i = 0; i < answers_sz; i++) {
        if (p1[i % 5== answers[i]) p_num[0]++;
        if (p2[i % 8== answers[i]) p_num[1]++;
        if (p3[i % 10== answers[i]) p_num[2]++;
    } 
    
    max = *max_element(p_num.begin(), p_num.end());
    for (int i = 0; i < 3; i++) {
        if (p_num[i] == max) answer.push_back(i+1);
    }
 
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

수포자마다 반복되는 패턴을 vector로 각자 만들어 answers와 비교해 각자 정답수를 체크해주면 되는 쉬운 문항

마지막에 최대값인 max와 같은 인덱스 번호에 + 1 하여(수포자가 1, 2, 3 이기 때문) answer 벡터에 넣어 반환해주면 된다

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

문자열 내 마음대로 정렬하기  (0) 2020.04.01
나누어 떨어지는 숫자 배열  (0) 2020.03.30
가운데 글자 가져오기  (0) 2020.03.30
K번째수  (0) 2020.03.30
완주하지 못한 선수  (0) 2020.03.30

https://programmers.co.kr/learn/courses/30/lessons/42576

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) {
 
    int participant_sz = participant.size();
    int completion_sz = completion.size();
    string answer = "";
    
    //vector를 sorting 후 안맞는 이름을 바로 출력
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
 
    for (int i = 0; i < completion_sz; i++) {
        if (participant[i] != completion[i]) {
            answer = participant[i];
            return answer;
        }
    }
    answer = participant[participant_sz-1];
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

completion의 길이가 participant 보다 길이가 1 작기 때문에 완주를 하지 못한 선수는 1명이다.

따라서 정렬된 두 vector를 비교하여 두 값이 달라지는 때의 participant의 값을 결과값으로 출력하면 된다.

 

입출력 예시를 봤을 때 입력되는 이름 vector가 정렬된 것이 아니기 때문에 sort() 함수를 통해 정렬하면 시간이 단축될 것이라고 판단

participant 와 completion 모두 정렬한 후 순서대로 비교를 해 값이 서로 다른 때의 participant의 값이 바로 완주하지 못한 선수가 된다.

 

 


 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

문자열 내 마음대로 정렬하기  (0) 2020.04.01
나누어 떨어지는 숫자 배열  (0) 2020.03.30
가운데 글자 가져오기  (0) 2020.03.30
K번째수  (0) 2020.03.30
모의고사  (0) 2020.03.30

+ Recent posts