카타 8급 If you can't sleep, just count sheep!!

1 C[ | ]

char* count_sheep(int n) {
  char* res = calloc(11*n, sizeof(char));
  for(int i=0; i<n; i++) sprintf(res+strlen(res), "%d sheep...", i+1);
  return res;
}
char* count_sheep(int n) {
  char* res = calloc(11*n, sizeof(char));
  char* p = res;
  for(int i=0; i<n; i++) {
    sprintf(p, "%d sheep...", i+1);
    while(*p) p++;
  }
  return res;
}

2 JavaScript[ | ]

var countSheep = function (num){
  let res = "";
  for(let i=1; i<=num; i++) res += `${i} sheep...`;
  return res;
}
var countSheep = function (num){
  var res = "";
  for(i=1; i<=num; i++) res += i+" sheep...";
  return res;
}

3 PHP[ | ]

function countsheep($num){
  $res = '';
  for($i=1; $i<=$num; $i++) $res .= "$i sheep...";
  return $res;
}

4 Python[ | ]

def count_sheep(n):
    res = ''
    for i in range(1,n+1):
        res += str(i) + ' sheep...'
    return res
def count_sheep(n):
    return ''.join(f"{i} sheep..." for i in range(1,n+1))
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}