一.快排
二.归并
三.钢条切割
1 #include<iostream> 2 using namespace std; 3 4 #define max(a,b) (((a) > (b)) ? (a) : (b)) 5 6 int Cut_Rod(int A[], int length) 7 { 8 if (A == NULL || length <= 0) 9 { 10 return 0; 11 } 12 if (length == 1) 13 { 14 return A[1]; 15 } 16 int q = -1; 17 for (int i = 2; i <= length; i++) 18 { 19 q = max(q, A[i - 1] + Cut_Rod(A, length - i)); 20 } 21 return q; 22 } 23 24 int main() 25 { 26 int A[] = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30}; 27 28 cout << Cut_Rod(A, 10) << endl; // 30 29 return 0; 30 }
四.矩阵链乘法
1 #include<iostream> 2 using namespace std; 3 4 inline int min(int a, int b) 5 { 6 if (a > b) 7 { 8 return b; 9 } 10 else 11 { 12 return a; 13 } 14 } 15 16 int Matrix_Chain_Order(int matrix[], int low, int high) 17 { 18 int q; 19 if (low == high) 20 { 21 return 0; 22 } 23 24 else if (low < high) 25 { 26 for (int i = low; i <= high - 1; i++) 27 { 28 if (i == low) 29 { 30 q = Matrix_Chain_Order(matrix, low, i) + Matrix_Chain_Order(matrix, i + 1, high) + matrix[low * 2 - 2] * matrix[i * 2 - 1] * matrix[high * 2 - 1]; 31 } 32 else 33 { 34 q = min(q, Matrix_Chain_Order(matrix, low, i) + Matrix_Chain_Order(matrix, i + 1, high) + matrix[low * 2 - 2] * matrix[i * 2 - 1] * matrix[high * 2 - 1]); 35 } 36 } 37 } 38 return q; 39 } 40 41 int main() 42 { 43 int A[] = { 30, 35, 35, 15, 15, 5, 5, 10, 10, 20, 20, 25}; 44 cout << Matrix_Chain_Order(A, 1, 6); //15125 45 cout << endl; 46 return 0; 47 }
五.LCS
1 #include<iostream> 2 using namespace std; 3 4 int max(int a, int b) 5 { 6 if (a > b) 7 { 8 return a; 9 } 10 else 11 { 12 return b; 13 } 14 } 15 16 int LCS(char x[], char y[], int i, int j) 17 { 18 if (x == NULL || y == NULL || i <= 0 || j <= 0) 19 { 20 return 0; 21 } 22 if (x[i - 1] == y[j - 1]) 23 { 24 return LCS(x, y, i - 1, j - 1) + 1; 25 } 26 else 27 { 28 return max(LCS(x, y, i - 1, j), LCS(x, y, i, j - 1)); 29 } 30 } 31 32 int main() 33 { 34 char x[] = "BDCABA"; 35 char y[] = "ABCBDAB"; 36 cout << LCS(x, y, 6, 7) << endl; //4 37 return 0; 38 }