카타 8급 Beginner - Lost Without a Map

Jmnote (토론 | 기여)님의 2019년 4월 15일 (월) 01:43 판 (→‎R)

1 C

#include <stddef.h>
int *maps(const int *arr, size_t size) {
  int *res = calloc(size, sizeof(int));
  for(int i=0; i<size; i++) res[i] = arr[i]*2;
  return res;
}

2 C++

std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res = values;
  for(int& v: res) v *= 2;
  return res;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector <int> v = values;
  std::transform(std::begin(v),std::end(v),std::begin(v),[](int x){return 2*x;});
  return v;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res;
  for(auto a:values) res.push_back(a*2);
  return res;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res = values;
  for(int& v: res) v *= 2;
  return res;
}

3 Kotlin

4 R

maps <- function(v) {
  v * 2
}