2022 KAKAO TECH INTERNSHIP-성격 유형 검사하기-C++

고동현·2024년 6월 3일
0

PS

목록 보기
31/51

확실히 인턴이 블라인드 채용보다는 문제가 쉬운느낌!

문제바로가기
딱히 설명할것은 없고 경우의수가 1,2,3,4,5,6,7밖에 없으므로 그냥 if문을 7개 만드는게 더 효율적이라고 생각해서 조건문을 각 경우마다 7개 -> 사실 4같은 경우에는 0점이니까 조건문 안만들어도됨

void init(){
    v.push_back({'R',0});
    v.push_back({'T',0});
    v.push_back({'C',0});
    v.push_back({'F',0});
    v.push_back({'J',0});
    v.push_back({'M',0});
    v.push_back({'A',0});
    v.push_back({'N',0});
}

각 알파벳에 맞는 숫자 pair로 둠

for(int i=0;i<survey.size();i++){
        char no = survey[i][0];
        char ok = survey[i][1];
        if(choices[i]==1){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second+=3;
        } 
        if(choices[i]==2){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second+=2;
        }
        if(choices[i]==3){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second++;
        }
        if(choices[i]==5){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second++;
        }
        if(choices[i]==6){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second+=2;
        }
        if(choices[i]==7){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second+=3;
        }
    }
    ..

for문을 돌면서 각 choices점수마다 해당 알파벳 찾아서 점수더하기

 
    if(v[0].second>=v[1].second){
        answer+='R';
    }else{
        answer+='T';
    }
    
    if(v[2].second>=v[3].second){
        answer+='C';
    }else{
        answer+='F';
    }
    
    if(v[4].second>=v[5].second){
        answer+='J';
    }else{
        answer+='M';
    }
    
    if(v[6].second>=v[7].second){
        answer+='A';
    }else{
        answer+='N';
    }
    return answer;

앞에게 뒤에것보다 크거나 같으면 answer에다가 넣어줌

전체 code

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

vector<pair<char,int>>v;

void init(){
    v.push_back({'R',0});
    v.push_back({'T',0});
    v.push_back({'C',0});
    v.push_back({'F',0});
    v.push_back({'J',0});
    v.push_back({'M',0});
    v.push_back({'A',0});
    v.push_back({'N',0});
}

string solution(vector<string> survey, vector<int> choices) {
    init();
    string answer = "";
    
    for(int i=0;i<survey.size();i++){
        char no = survey[i][0];
        char ok = survey[i][1];
        if(choices[i]==1){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second+=3;
        } 
        if(choices[i]==2){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second+=2;
        }
        if(choices[i]==3){
            auto it = find_if(v.begin(),v.end(),[&no](const pair<char, int>& elem){ return elem.first == no; });
            it -> second++;
        }
        if(choices[i]==5){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second++;
        }
        if(choices[i]==6){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second+=2;
        }
        if(choices[i]==7){
            auto it = find_if(v.begin(),v.end(),[&ok](const pair<char, int>& elem){ return elem.first == ok; });
            it -> second+=3;
        }
    }
    
    
    if(v[0].second>=v[1].second){
        answer+='R';
    }else{
        answer+='T';
    }
    
    if(v[2].second>=v[3].second){
        answer+='C';
    }else{
        answer+='F';
    }
    
    if(v[4].second>=v[5].second){
        answer+='J';
    }else{
        answer+='M';
    }
    
    if(v[6].second>=v[7].second){
        answer+='A';
    }else{
        answer+='N';
    }
    return answer;
}
profile
항상 Why?[왜썻는지] What?[이를 통해 무엇을 얻었는지 생각하겠습니다.]

0개의 댓글