카타 8급 Find the smallest integer in the array

1 C[ | ]

#include <stddef.h>
int find_smallest_int(int *vec, size_t len) {
  int min = vec[0];
  for(int i=1; i<len; i++) {
    if(vec[i] < min) min=vec[i];
  }
  return min;
}

2 C++[ | ]

#include <vector>
#include <algorithm>
using namespace std; 
int findSmallest(const vector <int>& list) {
  return *min_element(list.begin(), list.end());
}
#include <vector>
#include <algorithm>
using namespace std; 
int findSmallest(const vector <int>& list) {
  return *min_element(list.cbegin(), list.cend());
}
#include <vector>
using namespace std; 
int findSmallest(vector <int> list) {
  int min = list[0];
  for(int x: list) {
    if( x<min ) min=x;
  }
  return min;
}

3 Kotlin[ | ]

package solution
class SmallestIntegerFinder {
  fun findSmallestInt(nums: List<Int>) = nums.min()
}
package solution
class SmallestIntegerFinder {
  fun findSmallestInt(nums: List<Int>): Int {    
    return nums.min() ?: -1
  }
}
package solution
class SmallestIntegerFinder {
  fun findSmallestInt(nums: List<Int>): Int? {
    return nums.min()
  }
}
package solution
class SmallestIntegerFinder {
    fun findSmallestInt(nums: List<Int>): Int {
        var m = nums[0]
        nums.forEach {
            if( it<m ) m = it
        }
        return m
    }
}

4 PHP[ | ]

function smallestInteger ($arr) {
    return min($arr);
}
function smallestInteger ($arr) {
    $min = $arr[0];
    foreach($arr as $num) {
      if($num < $min) $min = $num;
    }
    return $min;
}

5 R[ | ]

findSmallestInt <- function(arr){
  min(arr)
}
findSmallestInt <- min
findSmallestInt <- function(arr){
  sort(arr)[1]
}
findSmallestInt <- function(arr) {
  min = arr[1]
  for(x in arr) {
    if(x < min) min = x
  }
  return(min)
}

6 같이 보기[ | ]