Coding Test Practice_4

2024. 11. 3. 21:43·Coding Test Practice

더 이상 미룰 수 없다 나의 공부...

 

LV.0 / JAVA   [PCCE 기출문제] 4번 / 병과분류

 

 

 

#include <iostream>

using namespace std;

int main(void) {
    string code;
    cin >> code;
    string last_four_words = code.substr(code.size()-4, 4);
    if(last_four_words == "_eye"){
        cout << "Ophthalmologyc";
    }
    else if(last_four_words == "head"){
        cout << "Neurosurgery";
    }
    else if(last_four_words == "infl"){
        cout << "Orthopedics";
    }
    else if(last_four_words == "skin"){
        cout << "Dermatology";
    }
    else{
        cout << "direct recommendation";
    }
    return 0;
}

 

이 문제는 쉬워서 괄호 한 번 깜빡한 것 말고는 한 번에 통과했다.

다음 문제까지 풀고 난이도를 높여볼까하는 생각이 들었다.

 

 

LV.0 / JAVA   [PCCE 기출문제] 5번 / 심폐소생술

 

 

  • 첫번째 풀이
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> cpr) {
    vector<int> answer = {0, 0, 0, 0, 0};
    vector<string> basic_order = {"check", "call", "pressure", "respiration", "repeat"};

    for(int i=0; i<answer.size(); i++){
        for(int j=0; j<basic_order.size(); j++){
            if(cpr[i] == basic_order[j]){
                answer[i] = basic_order[j];
                break;
            }
        }
    }
    return answer;
}

 

  • 문제 원인
/solution0.cpp:13:29: error: assigning to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' (aka 'int') from incompatible type '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' (aka 'std::__cxx11::basic_string<char>')
                answer[i] = basic_order[j];
                            ^~~~~~~~~~~~~~
1 error generated.

 

answer의 타입은 int 인데 넣으려고 한 basic_order의 타입은 string 이어서 에러가 났다. basic_order를 입력하는게 아니었나보다.

 

  • 두번째 풀이
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> cpr) {
    vector<int> answer = {0, 0, 0, 0, 0};
    vector<string> basic_order = {"check", "call", "pressure", "respiration", "repeat"};

    for(int i=0; i<cpr.size(); i++){
        for(int j=0; j<basic_order.size(); j++){
            if(cpr[i] == basic_order[j]){
                answer[i] = j+1;
                break;
            }
        }
    }
    return answer;
}

 

문제에서 cpr 요소에 따라 basic_order의 순서를 정하여 answer에 넣는 것이었기 때문에 외부 반복은 cpr의 각 요소에 대해 반복하도록 cpr의 크기를 사용하는 것으로 변경하였고.

일치하는 항목이 basic_order에서 발견되면 answer[i] 에 j+1 값을 설정하여 위치를 저장했다.

저작자표시 비영리 변경금지 (새창열림)

'Coding Test Practice' 카테고리의 다른 글

Coding Test Practice_3  (8) 2024.10.25
Coding Test Practice_2  (6) 2024.10.08
Coding Test Practice_1  (5) 2024.10.07
'Coding Test Practice' 카테고리의 다른 글
  • Coding Test Practice_3
  • Coding Test Practice_2
  • Coding Test Practice_1
hee-coding
hee-coding
  • hee-coding
    J의 코딩 일기
    hee-coding
  • 전체
    오늘
    어제
    • 분류 전체보기 (26)
      • FrontEnd (3)
        • HTML,CSS,Java Script (0)
        • React (3)
      • BackEnd (11)
        • DataBase (0)
        • JAVA (4)
        • Servlet & JSP (0)
        • Spring (7)
      • Project (1)
      • Error (1)
      • Git (0)
      • Coding Test Practice (4)
      • 일상~ (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    티스토리챌린지
    Coding
    오블완
    it
    코딩
    react
    frontend
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
hee-coding
Coding Test Practice_4
상단으로

티스토리툴바