프로그래머스 - 수박수박수박수박수박수?

well-life-gm·2021년 11월 6일
0

프로그래머스

목록 보기
44/125

프로그래머스 - 수박수박수박수박수박수?

"수박" 문자열을 n/2 만큼 더해주고, 홀수면 끝에 추가적으로 "수"를 붙여준다.

#include <string>
#include <vector>

using namespace std;

string solution(int n) {
    string answer = "";
    bool is_odd = n & 1 ? true : false;
    int end = n / 2;
    for(int i=0; i<end; i++) 
        answer += "수박";

    if(is_odd) 
        answer += "수";
    
    return answer;
}
profile
내가 보려고 만든 블로그

0개의 댓글