문제
풀이
#include <string>
#include <vector>
using namespace std;
string solution(int n) {
string answer = "";
for(int i=0; i<n-1; i+=2){
answer += "수박";
}
if(n%2==1) answer += "수";
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;
}
비트 연산자를 사용해 해결한 방법이다.
'Algorithm > Programers - C++' 카테고리의 다른 글
[프로그래머스] 약수의 합 (0) | 2022.02.17 |
---|---|
[프로그래머스]소수 찾기 / 에라토스테네스의 체 알고리즘 (0) | 2022.02.17 |
[프로그래머스]문자열 다루기 기본/isdigit (0) | 2022.02.14 |
[프로그래머스]문자열 다루기 기본 / multiset, sort (0) | 2022.02.12 |
[프로그래머스]문자열 내 p와 y의 개수 (0) | 2022.02.12 |