no image
[Level 1, C++] 문자열 내 마음대로 정렬하기
sort 함수를 이용하는데 정렬 기준을 내가 만들면 쉽게 만들 수 있다. #include #include #include using namespace std; int i; bool compare(string s1, string s2) { if(s1[i] == s2[i]) { if(s1 < s2) return true; else return false; } else if(s1[i] < s2[i]){ return true; } else return false; } vector solution(vector strings, int n) { i = n; vector answer = strings; sort(answer.begin(), answer.end(), compare); return answer; }
2022.07.03
no image
[Level 1, C++] 최소직사각형
모든 지갑을 같은 방향으로 만들어 놓으면 계산하기 편하다. #include #include using namespace std; int solution(vector sizes) { int answer = 0; int temp = 0; vector max(2, 0); for(int i=0; i
2022.07.03
no image
[Level 1, C++] 2016년
시간을 계산해주는 방법을 찾아서 2016년 1월 1일 부터 해당 날짜까지 시간을 계산해서 요일을 구한다. #include #include #include #include using namespace std; string solution(int a, int b) { string answer = ""; time_t start, end; struct tm s_time; int tm_day, tm_hour, tm_min, tm_sec; double diff; string day[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; s_time.tm_year = 2016 - 1900; s_time.tm_mon = 1 - 1; s_time.tm_mday = 1; s..
2022.07.03
no image
[Level 1, C++] 두 개 뽑아서 더하기
중복을 제거하는 함수인 unique와 erase를 같이 쓰면 쉽게 풀 수있다. #include #include #include using namespace std; vector solution(vector numbers) { vector answer; for(int i=0; i
2022.07.03
no image
[Level 1, C++] 3진법 뒤집기
파이썬에는 진법 계산하는 함수같은게 있던데 c++엔 그런게 없다. 그래서 그냥 직접 진법 계산하는걸로 만들었다. 직접 계산하면 앞뒤로 뒤집을 필요없으니 좋았다. #include #include #include #include using namespace std; int solution(int n) { int answer = 0; string str = ""; int i = 0; while (true) { str.append(to_string(n % 3)); n = n / 3; if( n == 0) break; } for (int i = 0; i < str.length(); i++) { cout
2022.07.03
no image
[Level 1, C++] 약수의 개수와 덧셈
다른사람들 풀이는 더 짧게 되어있어서 신기하게 느껴졌다. #include #include using namespace std; int solution(int left, int right) { int answer = 0; vector even; vector odd; vector temp; for (int i = left; i
2022.07.03
no image
[Level 1, C++] K번째수
잘라서 넣는 방법만 알면 쉽게 풀 수 있었다. #include #include #include using namespace std; vector solution(vector array, vector commands) { vector answer; for(int i = 0; i < commands.size(); i++) { vector copy; copy.assign(array.begin() + commands[i][0] - 1, array.begin() + commands[i][1]); sort(copy.begin(), copy.end()); answer.push_back(copy[commands[i][2] - 1]); } return answer; }
2022.07.03
no image
[Level 1, C++] 소수 만들기
문제를 봤을 때 "서로 다른 숫자 중 3개를 골라" 라고 나왔을 때 이건 조합을 이용해서 풀면 좋겠다고 싶었다. c++ STL에 조합을 만드는게 있었으면 좋았겠지만 순열을 만드는 것은 있다고 한다. 인터넷에 누가 올려주셔서 그냥 보고 따라했다. #include #include using namespace std; int solution(vector nums) { int answer = 0; vector arr; vectorv(nums.size() - 3, false); v.insert(v.end(), 3, true); do { vector temp; int sum = 0; for (int k = 0; k < nums.size(); k++) { if (v[k]) temp.push_back(nums[k]);..
2022.07.03
728x90
반응형

 

sort 함수를 이용하는데 정렬 기준을 내가 만들면 쉽게 만들 수 있다.


#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int i;

bool compare(string s1, string s2) {
    if(s1[i] == s2[i]) {
        if(s1 < s2) return true;
        else return false;
    }
    else if(s1[i] < s2[i]){
        return true;
    }
    else return false;
}

vector<string> solution(vector<string> strings, int n) {
    i = n;
    vector<string> answer = strings;
    sort(answer.begin(), answer.end(), compare);
    return answer;
}

 

728x90
반응형
728x90
반응형

 

모든 지갑을 같은 방향으로 만들어 놓으면 계산하기 편하다.


#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> sizes) {
    int answer = 0;
    int temp = 0;
    vector<int> max(2, 0);
    
    for(int i=0; i<sizes.size(); i++)
    {
        if(sizes[i][0] < sizes[i][1])
        {
            temp = sizes[i][0];
            sizes[i][0] = sizes[i][1];
            sizes[i][1] = temp;
        }
        
        for(int u=0; u<2; u++)
        {
            if(sizes[i][u] > max[u])
            {
                max[u] = sizes[i][u];
            }
        }
    }
    
    answer = max[0] * max[1];
    
    return answer;
}

 

728x90
반응형
728x90
반응형

 

시간을 계산해주는 방법을 찾아서 2016년 1월 1일 부터 해당 날짜까지 시간을 계산해서 요일을 구한다.

 


#include <string>
#include <vector>
#include <ctime>
#include <iostream>
using namespace std;

string solution(int a, int b) {
    string answer = "";
    
    time_t start, end;
    struct tm s_time;
    int tm_day, tm_hour, tm_min, tm_sec;
    double diff;
    
    string day[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
    
    s_time.tm_year = 2016 - 1900;
    s_time.tm_mon = 1 - 1;
    s_time.tm_mday = 1;
    s_time.tm_hour = 0, s_time.tm_min = 0, s_time.tm_sec = 0; s_time.tm_isdst = 0;
    
    start = mktime(&s_time);
    
    s_time.tm_year = 2016 - 1900;
    s_time.tm_mon = a - 1;
    s_time.tm_mday = b;
    
    end = mktime(&s_time);
    
    diff = difftime(end, start);
    tm_day = diff / (60 * 60 * 24);
    
    answer = day[(tm_day + 5) % 7];
    
    return answer;
}

 

728x90
반응형
728x90
반응형

 

중복을 제거하는 함수인 unique와 erase를 같이 쓰면 쉽게 풀 수있다.


#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(vector<int> numbers) {
    vector<int> answer;
    for(int i=0; i<numbers.size(); i++) {
        for(int u=0; u<numbers.size(); u++) {
            if(i == u) continue;
            answer.push_back(numbers[i] + numbers[u]);
        }
    }
    sort(answer.begin(), answer.end());
    answer.erase(unique(answer.begin(), answer.end()), answer.end());
    return answer;
}

 

728x90
반응형

'코딩 테스트 > 프로그래머스' 카테고리의 다른 글

[Level 1, C++] 최소직사각형  (0) 2022.07.03
[Level 1, C++] 2016년  (0) 2022.07.03
[Level 1, C++] 3진법 뒤집기  (0) 2022.07.03
[Level 1, C++] 약수의 개수와 덧셈  (0) 2022.07.03
[Level 1, C++] K번째수  (0) 2022.07.03
728x90
반응형

파이썬에는 진법 계산하는 함수같은게 있던데 c++엔 그런게 없다. 그래서 그냥 직접 진법 계산하는걸로 만들었다.

직접 계산하면 앞뒤로 뒤집을 필요없으니 좋았다.


#include <string>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

int solution(int n) {
    int answer = 0;
    string str = "";
    int i = 0;
    while (true) {
        str.append(to_string(n % 3));
        n = n / 3;
        if( n == 0) break;
    }
    for (int i = 0; i < str.length(); i++) {
        cout << str[str.length() - i - 1] << endl;
        answer += (str[str.length() - i - 1] - 48) * pow(3, i);
    }
    return answer;
}

 

728x90
반응형
728x90
반응형

다른사람들 풀이는 더 짧게 되어있어서 신기하게 느껴졌다.

#include <string>
#include <vector>

using namespace std;

int solution(int left, int right) {
    int answer = 0;
    vector<int> even;
    vector<int> odd;
    vector<int> temp;
    for (int i = left; i <= right; i++) {
        for (int u = 1; u <= i; u++) {
            if (i % u == 0) temp.push_back(u);
        }
        if (temp.size() % 2 == 0) even.push_back(i);
        else odd.push_back(i);
        temp.clear();
    }
    for (int & i : even) {
        answer += i;
    }
    for (int & i : odd) {
        answer -= i;
    }
    return answer;
}

 

728x90
반응형
728x90
반응형

잘라서 넣는 방법만 알면 쉽게 풀 수 있었다.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    for(int i = 0; i < commands.size(); i++) {
        vector<int> copy;
        copy.assign(array.begin() + commands[i][0] - 1, array.begin() + commands[i][1]);
        sort(copy.begin(), copy.end());
        answer.push_back(copy[commands[i][2] - 1]);
    }
    return answer;
}
728x90
반응형
728x90
반응형

문제를 봤을 때 "서로 다른 숫자 중 3개를 골라" 라고 나왔을 때 이건 조합을 이용해서 풀면 좋겠다고 싶었다.

c++ STL에 조합을 만드는게 있었으면 좋았겠지만 순열을 만드는 것은 있다고 한다.

인터넷에 누가 올려주셔서 그냥 보고 따라했다.

 

#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> nums) {
    int answer = 0;
    vector<int> arr;
    
    vector<bool>v(nums.size() - 3, false);
    v.insert(v.end(), 3, true);
    
    do {
        vector<int> temp;
        int sum = 0;
        for (int k = 0; k < nums.size(); k++) {
            if (v[k]) temp.push_back(nums[k]);
        }
        for(int & i : temp) {
            sum += i;
        }
        arr.push_back(sum);
        temp.clear();
    } while (next_permutation(v.begin(), v.end()));
    
    for(int & i : arr) {
        for(int u = 2; u <= i; u++) {
            if(i % u == 0 && i == u) {
                answer++;
            } 
            else if(i % u == 0 && i != u){
                break;
            }
        }
    }
    
    return answer;
}

 

728x90
반응형