카타 7급 The dropWhile Function

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:41 판 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

C[ | ]

#include <stdbool.h>
#include <stdlib.h>

typedef bool (*Predicate)(const int*);

int *dropWhile(
  const int *values, 
  size_t     count,
  Predicate  pred, 
  size_t     *pResultCount)
{
  for (; count && pred(values); ++values, --count) ;
  *pResultCount = count;
  size_t size = count * sizeof(int);
  return size ? memcpy(malloc(size), values, size) : NULL;
}
#include <stdbool.h>
#include <stdlib.h>

typedef bool (*Predicate)(const int*);

int *dropWhile(
  const int *values, 
  size_t     count,
  Predicate  pred, 
  size_t     *pResultCount)
{
  int *res = malloc(count*sizeof(int));
  int cnt = 0;
  for(int i=0; i<count; i++) {
    if(!pred(&values[i])) {
      for(int j=i; j<count; j++) res[cnt++] = values[j];
      break;
    }
  }
  *pResultCount = cnt;
  return realloc(res, cnt*sizeof(int));
}
#include <stdbool.h>
#include <stdlib.h>

typedef bool (*Predicate)(const int*);

int *dropWhile(
  const int *values, 
  size_t     count,
  Predicate  pred, 
  size_t     *pResultCount)
{
  const int *temp = values;
  int i = 0;
  for(; i<count; i++) {
    if( !pred(&values[i]) ) break;
  }
  int cnt = count-i;
  *pResultCount = cnt;
  if( cnt < 1 ) return NULL;
  int *result = malloc( (cnt+1)*sizeof(int) );
  for(int j=0; j<cnt; j++) {
    result[j] = values[i+j];
  }
  result[cnt] = '\0';
  return result;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}