본문 바로가기

컴퓨터공학

[프로그래머스, C++] 수박수박수박수박수박수?

반응형
#include <string>
#include <vector>

using namespace std;

string solution(int n) {
    string answer = "";
    for(int i=0;i<n/2;i++){
        answer.append("수박");
    }
    if(n%2!=0){
        answer.append("수");
    }
    return answer;
}

아래는 비트연산자와 &를 통한 코드이다.

#include <string>
#include <vector>

using namespace std;

string solution(int n) {
    string answer = "";

    for(int i = 0; i < n; i++)
        i & 1 ? answer += "박" : answer += "수";

    return answer;
}

삼항 연산자

조건식 ? 반환값1 : 반환값2

반응형