-
6. 수박수박수박수박수박수?코딩 테스트/Level 1 2019. 10. 4. 23:13반응형
https://programmers.co.kr/learn/courses/30/lessons/12922
파이썬
def solution(n): return '수박' * (n // 2) + ('수' if n % 2 else '')
자바스크립트
function solution(n) { return '수박'.repeat(n/2) + (n%2 ? '수' : ''); }
자바
class Solution { public String solution(int n) { return "수박".repeat(n/2) + ((n % 2 == 0) ? "" : "수"); } }
고
func solution(n int) (ret string) { for i := 0; i < n/2; i++ { ret += "수박" } if n%2 == 1 { ret += "수" } return }
func solution(n int) (answer string) { for i := 0; i < n/2; i++ { answer += "수박" } if n&1 == 1 { answer += "수" } return }
코틀린
class Solution { fun solution(n: Int) = "수박".repeat(n / 2) + if (n % 2 == 0) "" else "수" }
C#
public class Solution { public string solution(int n) { string answer = ""; for (int i = 0; i < (n / 2); i++) answer += "수박"; if (n % 2 == 1) answer += '수'; return answer; } }
public class Solution { public string solution(int n) { string answer = ""; for (int i = 0; i < n; i++) answer += (i % 2 == 0) ? '수' : '박'; return answer; } }
public class Solution { public string solution(int n) => string.Concat(new char[n / 2 + 1]).Replace("\0", "수박").Substring(0, n); }
DART
String solution(int n) { String answer = ''; for (int i = 0; i < n ~/ 2; i++) { answer += '수박'; } return answer + (n % 2 == 0 ? '' : '수'); }
String solution(int n) { return '수박' * (n ~/ 2) + (n % 2 == 0 ? '' : '수'); }
RUST
fn solution(n: i32) -> String { let mut result = "".to_owned(); for i in 0..n { result.push(match i % 2 { 0 => '수', _ => '박', }); } result }
반응형