본문 바로가기
Algorithm/C++

[프로그래머스 lv1] 서울에서 김서방 찾기

by imagineer_jinny 2022. 5. 18.

코딩테스트 연습 - 서울에서 김서방 찾기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 서울에서 김서방 찾기

String형 배열 seoul의 element중 "Kim"의 위치 x를 찾아, "김서방은 x에 있다"는 String을 반환하는 함수, solution을 완성하세요. seoul에 "Kim"은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니

programmers.co.kr

 

문제점

string에 int 넣는법 모름

 

해결

검색해서 찾음

to_string(number);

#include <string>
#include <vector>

using namespace std;

string solution(vector<string> seoul) {
    string answer = "";
    int index=0;
    for(int i=0;i<seoul.size();i++)
    {
        if(seoul[i]=="Kim")
        {
            index=i;
        }
    }
    
    answer+="김서방은 ";
    answer+=to_string(index);
    answer+="에 있다";
    
    return answer;
}

댓글