알고리즘/프로그래머스
프로그래머스 - 가운데 글자 가져오기
안알랴줌.
2019. 9. 6. 00:16
문제의 포인트는 문자열의 수가 짝수일때와 홀수일때를 어떻게 구분하고 처리할것인지
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
int loc=0;
if((s.size() %2) != 0) {
loc = (s.size() / 2) +1;
answer = s[loc-1];
}
else {
loc = s.size() /2;
answer = s[loc-1];
answer += s[loc];
}
return answer;
}