[[TableOfContents]] == 코딩테스트 1단계 == === 두 개 뽑아서 더하기 === {{{ #include #include #include 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; } }}} === 2016년 === {{{ #include #include #include #include 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; } }}} === 가운데 글자 가져오기 === {{{ #include #include #include #include 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; } } }}} === 두 정수 사이의 합 === {{{ #include #include #include long long solution(int a, int b) { long long answer = 0; int i; if (a>b){ int temp; temp = b; b = a; a = temp; } for (i = a; i <= b ; ++i){ answer += i; } return answer; } }}} === 문자열 내림차순으로 배치하기 === {{{ #include #include #include #include char* solution(const char* s) { int i,j, len =strlen(s); char* answer = (char*)malloc(sizeof(char)*len + 1); strcpy(answer,s); for(i = len; i > 0; --i){ for(j = 0; j < i - 1; ++j){ if (answer[j] < answer[j+1]){ char temp; temp = answer[j+1]; answer[j+1] = answer[j]; answer[j] = temp; } } } return answer; } }}} === 문자열 다루기 기본 === {{{ #include #include #include #include #include bool solution(const char* s) { bool t1, t2, answer; int i, count, len = strlen(s); if(len == 4 || len == 6){ t1 = true; }else{ t1 = false; } count = 0; for(i = 0 ;i < len; ++i){ if(isdigit(s[i])){ ++count; } } if(count == len){ t2 = true; }else{ t2 = false; } if(t1&&t2){ answer = true; }else{ answer = false; } return answer; } }}} === 수박수박수박수박수박수? === {{{ #include #include #include #include char* solution(int n) { // 리턴할 값은 메모리를 동적 할당해주세요. char* answer = (char*)malloc(sizeof("수") * n + 1); int i; memset(answer,'\0',sizeof("수") * n + 1); for(i = 0;i < n; ++i){ if(i%2==0){ strcat(answer,"수"); }else{ strcat(answer,"박"); } answer[n*sizeof("수")] = '\0'; } return answer; } }}} === 문자열을 정수로 바꾸기 === {{{ #include #include #include int solution(const char* s) { int answer = atoi(s); return answer; } }}} === 약수의 합 === {{{ #include #include #include int solution(int n) { int i, answer = 0; for(i = 1; i <= n; ++i){ if(n%i == 0){ answer += i; } } return answer; } }}}