본문 바로가기

TEST/자료구조와 함께 배우는 알고리즘 입문(C언어)

(73)
<8> Q10. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include int bf_match(const char txt[], const char pat[]) { int pt = 0; int pp = 0; int ans = 0; while (txt[pt] != '\0') { if (txt[pt] == pat[pp]) { pt++; pp++; } else { pt = pt - pp + 1; pp = 0; } if (pat[pp] == '\0') { ans = pt - pp; pt = pt - pp + 1; pp = 0; } } if (ans == 0) return -1; return ans; } int main(void) { int idx; char s1[256]; char s2[256]; puts("브루트-포스법"); printf("텍스트 : "); scan..
<8> Q9. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include int bf_match(const char txt[], const char pat[]) { int pt = 0; int pp = 0; int count = 0; while (txt[pt] != '\0' && pat[pp] != '\0') { //print if (pp == 0) { printf("%d ", pt); for (int i = 0; txt[i] != '\0'; i++) printf("%c", txt[i]); } else { printf(" "); for (int i = 0; txt[i] != '\0'; i++) printf("%c", txt[i]); } printf("\n"); //print if (txt[pt] == pat[pp]) { //print for (int i = 0..
<8> Q8. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include int str_cmpic(const char *s1, const char *s2) { while ((*s1 == *s2) || ((*s1 + 32 == *s2) || *s1 == *s2 + 32)) { if (*s1 == '\0') return 0; s1++; s2++; } return (unsigned char)*s1-(unsigned char)*s2; } int str_ncmpic(const char *s1, const char *s2, size_t n) { for (size_t i = 0; i < n; i++) { if ((s1[i] != s2[i]) && ((s1[i] + 32 != s2[i]) && (s1[i] != s2[i] + 32))) { return s1[..
<8> Q7. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include int str_ncmp(const char *s1, const char *s2, size_t n) { for(size_t i = 0; i
<7> Q2. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include typedef struct { int max; int num; int *set; } IntSet; int InputSet(IntSet *s) { int i, j, k, a, b, max, temp; printf("원소의 개수 입력: "); scanf("%d", &s->max); if ((s->set = calloc(s->max, sizeof(int))) == NULL) { s->max = 0; return -1; } s->num = 0; for (int i = 0; i max; i++) { printf("%d(input 0 = exit) : ", i + 1); scanf("%d", &s->set[i]); if (s->set[i] == 0) break; s->num..
<7> Q1. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include typedef struct { int max; int num; int *set; } IntSet; int Initialize(IntSet *s, int max) { s->num = 0; if ((s->set = calloc(max, sizeof(int))) == NULL) { s->max = 0; return -1; } s->max = max; return 0; } int IsMember(const IntSet *s, int n) { int i; for (i = 0; i num; i++) if (s->set[i] == n) return i; return -1; } void Add(IntSet *s, int n) { if (s->num max && IsMe..
<6> Q24. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include void fsort2(int a[], int n, int min, int max) { int i; int *f = calloc(max - min + 2, sizeof(int)); int *b = calloc(n, sizeof(int)); for (i = 0; i max); } fsort2(x, nx, min, max); puts("오름차순으로 정렬했습니다."); for (i = 0; i < nx; i++) printf("x[%d] = %d\n", i, x[i]); free(x); return 0; }
<6> Q23. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include void fuprint(int a[], int b[], int f[], int n, int max) { printf("a "); for (int i = 0; i < n; i++) printf("%3d", a[i]); printf("\n"); printf("f "); for (int i = 0; i