카타 8급 I love you, a little , a lot, passionately ... not at all

1 C[ | ]

#include <stddef.h>
const char* how_much_i_love_you(int nb_petals) {
  switch(nb_petals%6) {
    case 1: return "I love you";
    case 2: return "a little";
    case 3: return "a lot";
    case 4: return "passionately";
    case 5: return "madly";
    case 0: return "not at all";
  }
  return "";
}
#include <stddef.h>
const char* how_much_i_love_you(int nb_petals) {
  const char *phrases[6] = {
    "I love you",
    "a little",
    "a lot",
    "passionately",
    "madly",
    "not at all"
  };
  return phrases[(nb_petals - 1) % 6];
}

2 C++[ | ]

std::string how_much_i_love_you(int nb_petals) {
  switch(nb_petals%6) {
    case 1: return "I love you";
    case 2: return "a little";
    case 3: return "a lot";
    case 4: return "passionately";
    case 5: return "madly";
    case 0: return "not at all";
  }
}
std::string how_much_i_love_you(int nb_petals) {
    string arr[] = {"I love you", "a little", "a lot", "passionately", "madly", "not at all"};
    return arr[(nb_petals - 1) % 6];
}

3 PHP[ | ]

function how_much_i_love_you(int $nb_petals): string {
  switch($nb_petals%6) {
    case 1: return 'I love you';
    case 2: return 'a little';
    case 3: return 'a lot';
    case 4: return 'passionately';
    case 5: return 'madly';
    case 0: return 'not at all';
  }
}
function how_much_i_love_you(int $nb_petals): string {
  return ["I love you", "a little", "a lot", "passionately", "madly", "not at all"][($nb_petals - 1) % 6];
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}