반응형
#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
반응형
'컴퓨터공학' 카테고리의 다른 글
[프로그래머스, C++] 예산 (0) | 2021.09.26 |
---|---|
[프로그래머스, C++] 없는 숫자 더하기 (0) | 2021.09.26 |
[프로그래머스/ C++]두 정수 사이의 합 (0) | 2021.09.19 |
[프로그래머스/C++] 약수의 개수와 덧셈 (0) | 2021.09.19 |
[프로그래머스/C++] 약수의 합 (0) | 2021.09.12 |