[프로그래머스] 12922 수박수박수박수박수박수?
프로그래머스 level1
문제
문제 풀이
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public String solution(int n) { | |
char[] array = new char[n]; | |
for (int i = 0; i < n; i++) { | |
if (i % 2 == 0) { | |
array[i] = '수'; | |
} else { | |
array[i] = '박'; | |
} | |
} | |
return new String(array); | |
} | |
} |
문제 리뷰
입력된 n의 크기에 맞게 수박을 연이어 출력하는 문제다.
0, 2, 4..와 같은 짝수번째의 순서에는 ‘수’,
1, 3, 5..와 같은 홀수번째 순서에는 ‘박’이 들어가는 규칙을 생각했다.
그래서 나는 char 배열을 n개만큼 생성하고
반복문을 통해 배열의 인덱스에 맞게 ‘수’ / ‘박’을 넣어줬다.
이 배열의 반환값은 String이기 때문에
char 배열을 String으로 변환하여 반환해줬다.
TMI
풀다보니 로꾸꺼가 생각난다..
수박이 박수🍉
1일 1알고리즘 완료🤓
댓글남기기