728x90
www.hackerrank.com/challenges/the-birthday-bar/problem
m(month)와 d(day) 값을 이용해서 정수배열 s에서 연속된 m개의 숫자의 합이 d인 경우의 수를 출력하는 코드이다.
전체적으로 쉽고 주의해야 할 부분은 반복문의 횟수(조건 부분)이다.
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);
// Complete the birthday function below.
int birthday(int s_count, int* s, int d, int m) {
int gift=0;
int sum=0;
for(int i=0;i<s_count-m+1;i++){
sum = 0;
for(int j=0;j<m;j++){
sum += s[i+j];
}
if(sum == d) gift++;
}
return gift;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* n_endptr;
char* n_str = ltrim(rtrim(readline()));
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
char** s_temp = split_string(rtrim(readline()));
int* s = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
char* s_item_endptr;
char* s_item_str = *(s_temp + i);
int s_item = strtol(s_item_str, &s_item_endptr, 10);
if (s_item_endptr == s_item_str || *s_item_endptr != '\0') { exit(EXIT_FAILURE); }
*(s + i) = s_item;
}
int s_count = n;
char** dm = split_string(rtrim(readline()));
char* d_endptr;
char* d_str = dm[0];
int d = strtol(d_str, &d_endptr, 10);
if (d_endptr == d_str || *d_endptr != '\0') { exit(EXIT_FAILURE); }
char* m_endptr;
char* m_str = dm[1];
int m = strtol(m_str, &m_endptr, 10);
if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILURE); }
int result = birthday(s_count, s, d, m);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
SMALL
'Programming > C C++' 카테고리의 다른 글
Divisible Sum Pairs (0) | 2020.09.13 |
---|---|
[DS] Insert a node at the head of a linked list (0) | 2020.08.30 |
[DS] Insert a Node at the Tail of a Linked List (0) | 2020.08.23 |
Breaking the Records (0) | 2020.08.23 |
Print the Elements of a Linked List (0) | 2020.08.16 |