카타 7급 Enumerable Magic #5- True for Just One?

C[ | ]

#include <stdbool.h>
#include <stddef.h>
typedef bool (*Predicate)(int);
bool one(const int* arr, size_t size, Predicate fun)
{
  bool exist = false;
  for(size_t i=0; i<size; i++) {
    if(fun(arr[i])) {
      if(exist) return false;
      exist = true;
    }
  }
  return exist;
}
#include <stdbool.h>
#include <stddef.h>
typedef bool (*Predicate)(int);
bool one(const int* arr, size_t size, Predicate fun)
{
  int count = 0;
  for(int i=0; i<size; i++) {
    if( fun(arr[i]) ) count++;
    if( count > 1 ) return false;
  }
  return count == 1;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}