U E D R , A S I H C RSS

새싹교실/2020/이찌반/정동원 (rev. 1.11)

새싹교실/2020/이찌반/정동원



1. 코딩테스트 1단계


1.1. 2016년

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

char* solution(int a, int b) {
    int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
    char week[7][4] = {"FRI","SAT","SUN","MON","TUE","WED","THU"};
    char* answer = (char*)malloc(sizeof(char)*4);

    int i, d, n=-1;

    for (i = 0; i < a - 1; ++i) {
        n += days[i];
    }
    n += b;
    d = n % 7;

    strcpy(answer, week[d]);
    return answer;
}

1.2. 두 개 뽑아서 더하기

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

int compare(void *a,void *b);
int* solution(int numbers[], size_t numbers_len) {

    qsort(numbers,numbers_len,sizeof(int),compare);
    int* temp = (int*)malloc(sizeof(int)*201);
    memset(temp,0,sizeof(int));
    
    int i,j,k,n=0;
    for (i = 0; i < numbers_len - 1; ++i){
        for(j = i+1; j < numbers_len; ++j){
            int count = 0;
            for(k=0; k < n; ++k){
                if(temp[k] == numbers [i] + numbers[j]){
                    ++count;
                }
            }
            if(count == 0){
                temp[n] = numbers [i] + numbers[j];
            ++n;
            }           
        }
    }

    qsort(temp,n,sizeof(int),compare);
    int* answer = (int*)malloc(sizeof(int)*n);
    for (i = 0; i < n; ++i){
        answer[i]= temp[i];
    }
    free(temp);
    return answer;
}

int compare(void *a,void *b){
    int num1 = *(int *)a;
    int num2 = *(int *)b;
    return num1 - num2;
}

1.3. 가운데 글자 가져오기

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

char* solution(const char* s) {
    int len = strlen(s);
    if (len % 2) {
        char* answer = (char*)malloc(sizeof(char) * 2);
        answer[0] = s[len / 2];
        answer[1] = '\0';
        printf("%s", answer);
        return answer;
    }else{
        char* answer = (char*)malloc(sizeof(char) * 3);
        answer[0] = s[len / 2 - 1];
        answer[1] = s[len / 2];
        answer[2] = '\0';
        printf("%s", answer);
        return answer;
    }
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-16 14:07:54
Processing time 0.0167 sec