카타 8급 Counting sheep...

1 C++[ | ]

#include <algorithm>
using namespace std; 
int count_sheep(vector<bool> arr) {
  return count(arr.cbegin(), arr.cend(), true);
}
#include <algorithm>
using namespace std; 
int count_sheep(vector<bool> arr) {
  return count(arr.begin(), arr.end(), true);
}
using namespace std; 
int count_sheep(vector<bool> arr) {
  int res = 0;
  for(bool x: arr) {
    if( x ) res++;
  }
  return res;
}

2 Java[ | ]

public class Counter {
  public int countSheeps(Boolean[] arrayOfSheeps) {
    int count = 0;
    for (Boolean b : arrayOfSheeps) if (b) count++;
    return count;
  }
}

3 Python[ | ]

def count_sheeps(arrayOfSheeps):
  return arrayOfSheeps.count(True)
def count_sheeps(arrayOfSheeps):
  return len([x for x in arrayOfSheeps if x])
def count_sheeps(arrayOfSheeps):
  count = 0
  for sheep in arrayOfSheeps:
    if sheep:
      count += 1
  return count
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}