"카타 7급 The dropWhile Function"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
1번째 줄: 1번째 줄:
==C==
==C==
{{카타|7급|C|2}}
{{카타|7급|C|2}}
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdbool.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdlib.h>
18번째 줄: 18번째 줄:
   return size ? memcpy(malloc(size), values, size) : NULL;
   return size ? memcpy(malloc(size), values, size) : NULL;
}
}
</source>
</syntaxhighlight>
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdbool.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdlib.h>
42번째 줄: 42번째 줄:
   return realloc(res, cnt*sizeof(int));
   return realloc(res, cnt*sizeof(int));
}
}
</source>
</syntaxhighlight>
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdbool.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdlib.h>
70번째 줄: 70번째 줄:
   return result;
   return result;
}
}
</source>
</syntaxhighlight>

2020년 11월 2일 (월) 02:41 기준 최신판

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 }}