[Programmers] #12922 - 수박수박수박수박수박수? [Java][C++][Python]
[Programmers] #12922 - 수박수박수박수박수박수? [Java][C++][Python]
1. 아이디어
"수박" 두 글자가 반복되는 길이 $n$의 문자열을 만드는 문제로, $n$을 2로 나눈 몫만큼 "수박"을 이어 붙이고 $n$이 홀수일 때만 끝에 "수" 하나를 더 붙이면 길이가 $n$이 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public String solution(int n) {
return "수박".repeat(n / 2) + "수".repeat(n % 2);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
string solution(int n) {
string s;
for (int i = 0; i < n / 2; i++) {
s += "수박";
}
if (n % 2) s += "수";
return s;
}
1
2
def solution(n):
return "수박" * (n // 2) + "수" * (n % 2)
This post is licensed under CC BY 4.0 by the author.