본문 바로가기

알고리즘8

[알고리즘] Assignment 5 - LCS 알고리즘, BFS 알고리즘, Kruskal 알고리즘, Dijkstra 알고리즘, Prim 알고리즘 1. LCS (Longest Common Subsequence, 최장 공통 부분 문자열) #include #define MAX(a,b) ((a)>(b)? (a):(b)) int LCS_table[10][10] = { 0, }; void LCS_length(int str1_len, int str2_len, char* str1, char* str2) { for (int i = 1; i < str1_len; i++) { for (int j = 1; j < str2_len; j++) { if (str1[i] == str2[j]) { LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1; } else { LCS_table[i][j] = MAX(LCS_table[i - 1][j], LC.. 2022. 1. 28.
[알고리즘] Assignment 3 - 정렬 알고리즘 (bubble sort, insertion sort, merge sort, quick sort, radix sort, bucket sort) 1. Code #include #include #define SIZE 1000 #define DIGIT 4 int origin[SIZE], result[SIZE], result_2[SIZE]; void bubble_sort() { int i, j, temp; for (i = SIZE - 1; i > 0; i--) { for (j = 0; j origin[j + 1]) { temp = origin[j]; origin[j] = origin[j + 1]; origin[j + 1] = temp; } } } } void insertion_sort() { int i, j, temp; for (i = 1; i < SIZE; i++) { temp = origin[i];.. 2022. 1. 28.
[알고리즘] Assignment 2 Submit a word, hwp or pdf file to the e-class that contains (1) your answers for the following exercises, and (2) your codes written in the C programming language and their results for programming exercises (screenshots okay). 1. (programming) Write a program that takes a number n and displays the largest k satisfying the following equations: 2^k 2022. 1. 28.
[알고리즘] Assignment 1 2022. 1. 28.
[자료구조 프로그래밍 연습문제] 힙 정렬 (heap sort) heap sort A. 배열 a[1]에서 a[11]에 11개의 999보다 큰 네자리 이상의 양의 정수가 저장되어 있다고 하자. 교재 Program 7.13: Heap sort의 함수 heapSort를 호출하여 이들을 오름순으로 정렬하는 과정을 확인하고자 한다. 이를 위해 Program 7.13에서 함수 adjust가 호출될 때마다 그 호출 직후 시점의 배열 a[1] 부터 a[11] 까지의 값을 출력하시오. 또한 정렬 진행 과정 설명을 위해 자신이 확인하고 싶은 기타 변수 값이 있으면 출력하시오. 출력된 값을 보고 배열을 complete binary tree로 그림을 그려서 해당 adjust의 수행 결과와 정렬 진행 과정에 대해 설명하시오. (complete binary tree 그림은 육필로 그려도 되고.. 2022. 1. 28.
[자료구조 프로그래밍 연습문제] 재귀함수, 선택정렬, 배열 1. recursion 교재 Figure 1.3의 C 코드에서 수업시간에 언급된 오류를 차고 정확한 코드로 수정하시오. 수정하지 않을 경우 실행 결과에 어떤 오류가 발생하는지 간단한 예를 들어 설명하시오. float rsum(float list[], int n) { if (n) return rsum(list, n-1) + list[n-1]; return 0; } 0 대신 list[0]의 값을 반환하면, list배열의 값들의 전체 합에 첫번째 요소 값이 한 번 더 더하여져서 값이 달라진다. 예를 들어, list == [1, 2, 3, 4, 5] 인 경우, rsum의 결과 값은 1 + 1 + 2 + 3 + 4+ 5, 16으로 첫번째 요소인 1만큼의 오차가 발생한다. 2. recursive implementa.. 2022. 1. 10.
[C 프로그래밍 실습] 동적 메모리와 전처리 2 (Lab 14) Program 1 : 전처리 함수, 매크로 Program 2 : 전처리 함수, 매크로 Program 3 : 전처리 함수, 매크로, 삼항 연산자 Program 4 : 버블 정렬 Program 5 : 선택 정렬 Program 6 : 삽입 정렬 Program 1 다음을 참고로 매크로 PRINTM(exp)를 정의하여 다음 결과가 나오도록 프로그램을 작성하시오. Dev C++ 의 경우 전처리 연산자(#, #@, ##)는 printf() 인자로 사용할 경우 오류 발생하나, 아래 문제를 해결하기 위한 매크로 정의에는 문제 없음 매크로 PRINTM (exp)는 “Expression: exp = 연산 결과값” 으로 출력 int a = 2; PRINTM(3 * 4 + 3 / a); #include #define PRINT.. 2021. 1. 8.
[프로그래밍언어론] 과제 1 : 이터레이터 생성자 (Iterator Generator) 만들기 문제 Develop a program per each of the examples of the Python iterator generator for “sort”(quicksort) and “flatten” in ANY language of choice with the following constraints: You MUST NOT use any constructs that are similar or equivalent to the iterator generator of Python (such as “yield”). You have only to use ordinary control structures (if, while, ...) and/or procedure calls. You CAN utilize o.. 2020. 12. 20.