💡 재귀, 정렬, 해시
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int hasIT(const char *list, const char alpha) {
for(int i=0; ;i++) {
char c = list[i];
if(!c) break;
if(c == alpha) return 1;
}
return 0;
}
char* solution(const char* my_string) {
int l = strlen(my_string);
char* answer = (char*)calloc(l+1, sizeof(char));
int countA = 0;
for(int i=0; i<l ;i++) {
char c = my_string[i];
if(!hasIT(answer, c)) answer[countA++] = c;
}
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(const char* letter) {
int l = strlen(letter);
char* cpyLetter = (char*)calloc(l+1, sizeof(char));
strcpy(cpyLetter, letter);
char* answer = (char*)calloc(l+1, sizeof(char));
char* ptr = 0;
int countA = 0;
const char* morse[] = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
ptr = strtok(cpyLetter, " ");
while (ptr) {
printf("%s\n", ptr);
for (int i = 0; i < 26; i++) {
if (!strcmp(morse[i], ptr)) {
answer[countA++] = i + 'a';
break;
}
}
ptr = strtok(NULL, " ");
}
return answer;
}
2차원 동적 배열만들기 (포인터의 배열을 만들고, 각 포인터에 몸체를 붙여넣는다)
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int** solution(int num_list[], size_t num_list_len, int n) {
int row = num_list_len / n;
int** answer = (int**)calloc(row, sizeof(int *));
for(int i=0; i<row; i++) {
answer[i] = (int*)calloc(n, sizeof(int));
for(int j=0; j<n; j++){
answer[i][j] = num_list[i*n + j];
}
}
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int n) {
int answer = 1;
int facto = 1;
for( ; ; answer++) {
facto *= answer;
if(facto > n) break;
}
return answer-1;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int compare(char *a, char *b) {
return *a - *b;
}
int solution(const char* before, const char* after) {
int answer = 0;
int lb = strlen(before);
int la = strlen(after);
char *b = (char *)calloc(lb+1, sizeof(char));
char *a = (char *)calloc(la+1, sizeof(char));
strcpy(b, before);
strcpy(a, after);
qsort(b, lb, sizeof(char), compare);
qsort(a, la, sizeof(char), compare);
printf("%s : %s", b, a);
if(strcmp(b,a) == 0) answer = 1;
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int array[], size_t array_len, int n) {
int answer = 0;
int min = 100;
for(int i=0; i<array_len; i++) {
int gap = array[i] - n; // n과의 거리 저장
if(gap < 0) gap *= -1; // gap이 음수이면, 양수로 변환
if(gap == min) { // 이미 최소거리가 있더라도, 작은 수라면 교체해야 함
if(answer > array[i]) {
answer = array[i];
}
} else if(gap < min) { // 현재 최소거리보다 더 가까운 수가 있으면 교체해야 함
min = gap;
answer = array[i];
}
}
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int i, int j, int k) {
int answer = 0;
for(int num=i; num<=j; num++) {
int n = num;
for( ; n ; n/=10) {
if(n%10 == k) answer++;
}
}
return answer;
}
구조체 필요
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct _patient {
int em;
int index;
} P;
int compare(P *a, P *b) {
// return (*b).em - (*a).em;
return b->em - a->em;
}
int* solution(int emergency[], size_t emergency_len) {
int l = emergency_len;
int* answer = (int*)calloc(l, sizeof(int));
P* patient = (P*)calloc(l, sizeof(P));
for(int i=0; i<l; i++) {
patient[i].em = emergency[i];
patient[i].index = i;
}
qsort(patient, l, sizeof(P), compare);
for(int i=0; i<l; i++) {
answer[patient[i].index] = i+1;
}
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int solution(const char* my_string) {
int answer = 0;
int l = strlen(my_string);
char *str = (char*)calloc(l+1, sizeof(char));
strcpy(str, my_string);
char *ptr;
char *delimeter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
ptr = strtok(str, delimeter);
while(ptr) {
answer += atoi(ptr);
ptr = strtok(NULL, delimeter);
}
return answer;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
char* solution(const char* s) {
int l=strlen(s);
char* answer = (char*)calloc(l+1, sizeof(char));
int* alpha = (int*)calloc(26, sizeof(int));
int countA=0;
for(int i=0; i<l; i++) { // 한번 쭉 읽으면서 alphabet 출현 횟수 카운트
char c = s[i];
alpha[c-'a']++;
}
for(int i=0; i<26; i++) { // 알파벳 출현횟수가 1인 경우만 answer에 덧붙여주기
if(alpha[i] == 1)
answer[countA++] = i + 'a';
}
return answer;
}