카타 7급 Sum of the first nth term of Series

C[ | ]

#include <stddef.h>
char *series_sum(const size_t n) {
  float sum = 0;
  for(int i=0; i<n; i++) {
    sum += 1.0 / (1.0+i*3.0);
  }
  char* s="";
  asprintf(&s, "%.2f", sum);
  return s;
}
#include <stddef.h>
char *series_sum(const int n) {
  double sum = 0;
  for (double d = 1 + 3 * ((double)n - 1); d >= 1; d -= 3)
    sum += 1 / d;
  char *res = NULL;
  asprintf(&res, "%.2f", sum);
  return res;
}
#include <stddef.h>
char *series_sum(const size_t n) {
  double result = n ? 1. : 0.;
  char *s = (char *)malloc(10 * sizeof(char));
  for (int i = 1; i < n; i++) result += 1. / (3 * i + 1);
  sprintf(s, "%.2lf", result);
  return s;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}