카타 8급 Fake Binary

1 C[ | ]

void fakeBin(const char *digits, char *buffer) {
  int len = strlen(digits);
  for(int i=0; i<len; i++) buffer[i] = digits[i]<'5'?'0':'1';
  buffer[len] = '\0';
}
void fakeBin(const char *digits, char *buffer) {
  for(int i=0; digits[i]; i++) *buffer++ = digits[i] >= '5' ? '1' : '0';
  *buffer = 0;
}

2 C++[ | ]

#include <string>
#include <regex>
std::string fakeBin(std::string str){
  str = regex_replace(str, std::regex("[1-4]"), "0");
  str = regex_replace(str, std::regex("[5-9]"), "1");
  return str;
}
#include <string>
std::string fakeBin(std::string str){
  for (int i=0; i<str.length(); i++) str[i] = (str[i]<'5') ? '1' : '0';
  return str;
}
#include <string>
std::string fakeBin(std::string str){
  std::string res;
  for(char x: str) {
    if( x < '5' ) res += "0";
    else res += "1";
  }
  return res;
}

3 PHP[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}