TEST/자료구조와 함께 배우는 알고리즘 입문(C언어) (73) 썸네일형 리스트형 <5> Q9. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include int flag_a[8]; int flag_b[15]; int flag_c[15]; int pos[8]; typedef struct { int max; int prt; int *stk; } IntStack; int Initialize(IntStack *s, int max) { s->prt = 0; if ((s->stk = (int *)calloc(max, sizeof(int))) == NULL) { s->max = 0; return -1; } s->max = max; return 0; } int Push(IntStack *s, int x) { if (s->prt >= s->max) return -1; s->stk[s->prt++] = x; return 0; } int Po.. <5> Q8. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include int flag_a[8]; int flag_b[15]; int flag_c[15]; int pos[8]; void print(void) { int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (pos[j] == i) printf("■"); else printf("☐"); } putchar('\n'); } } void set(int i) { int j; for (j = 0; j < 8; j++) { if (!flag_a[j] && !flag_b[i + j] && !flag_c[i - j + 7]) { pos[i] = j; if (i == 7) print(); else { flag_a[j] = flag_b[i + j] = fla.. <5> Q7. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include #include typedef struct { int max; int prt; int *stk; } IntStack; char word(int n) { switch (n) { case 1: return 'A'; case 2: return 'B'; case 3: return 'C'; } } int Initialize(IntStack *s, int max) { s->prt = 0; if ((s->stk = (int *)calloc(max, sizeof(int))) == NULL) { s->max = 0; return -1; } s->max = max; return 0; } int Push(IntStack *s, int x) { if (s->prt >= s->max) return -1; s->.. <5> Q6. 자료구조와 함께 배우는 알고리즘 입문 (C언어) 정수값을 받아서 문자로 반환하는 함수를 만들어서 사용함 #include char word(int n) { switch(n){ case 1: return 'A'; case 2: return 'B'; case 3: return 'C'; } } void move(int no, int x, int y) { if(no>1) move(no-1, x,6-x-y); printf("원반[%d]를 %c 기둥에서 %c 기둥으로 옮김\n",no,word(x),word(y)); if(no>1) move(no-1,6-x-y,y); } int main(void) { int n; printf("하노이의 탑\n원반 개수 : "); scanf("%d",&n); move(n,1,3); return 0; } <5> Q5. 자료구조와 함께 배우는 알고리즘 입문 (C언어) 스택 구조 a에는 n값 b에는 구분값을 넣어서 첫번째 항 값이였는지 두번째 항 값이였는지 구분해줌 #include #include typedef struct { int max; int prt; int *stk; } IntStack; int Initialize(IntStack *s, int max) { s->prt = 0; if ((s->stk = (int *)calloc(max, sizeof(int))) == NULL) { s->max = 0; return -1; } s->max = max; return 0; } int Push(IntStack *s, int x) { if (s->prt >= s->max) return -1; s->stk[s->prt++] = x; return 0; } int Pop(In.. <5> Q4. 자료구조와 함께 배우는 알고리즘 입문 (C언어) <5> Q3. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include int gcd_array(const int a[], int n) { int max = 0, min = 0, num = 0; for (int i = 0; i min) { min = a[i]; } } } for (; min > 0; min--) { num = 0; for (int i = 0; i < n; i++) { if (a[i] % min == 0) num++; } if (num == n) return min; } return 0; } int main(void) { int a[5] = {}, n = 5; for (int i = 0; i < 5; i++) { printf("Input N.. <5> Q2. 자료구조와 함께 배우는 알고리즘 입문 (C언어) #include int gcd(int x, int y) { int temp; while (1) { if (y == 0) { return x; } else { temp = x; x = y; y = temp % y; } } } int main(void) { int num1, num2; printf("Input two two Number : "); scanf("%d %d", &num1, &num2); printf("최대공약수는 %d입니다", gcd(num1, num2)); } 이전 1 2 3 4 5 6 7 ··· 10 다음 목록 더보기