레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) {
    string answer = "";
    
    int strSize = s.length();
    
    if(strSize % 2 == 0// 짝수
    {
        answer += s.at((strSize / 2-1);
        answer += s.at(strSize / 2);
    }
    else
        answer = s.at(strSize / 2);
        
    
    
    return answer;
}
 

내가 푼것.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) 
{
   int strSize = s.length();
    
    if(strSize % 2 == 0// 짝수
        return s.substr((strSize * 0.5)-1 , 2); 
    else
        return s.substr(strSize * 0.5);
 
    return string();
}
 

모범 답안

string class의 substr을 사용한다.

substr(출력하고싶은 문자열 시작위치, 출력갯수);

+ Recent posts