카타 8급 Beginner - Lost Without a Map

Jmnote (토론 | 기여)님의 2019년 4월 21일 (일) 02:22 판 (→‎Kotlin)

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

fun maps(x: IntArray): IntArray {
    return x.map { it * 2 }.toIntArray()
}
fun maps(x : IntArray) = x.map { it * 2 }.toIntArray()
fun maps(x: IntArray): IntArray {
    for (i in 0 until x.size) x[i] *= 2
    return x
}

4 R

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