C语言实现最长递增子序列
时间: 2024-04-14 09:22:00 浏览: 137
以下是C语言实现最长递增子序列问题的解决方法:
```c
#include <stdio.h>
int max(int a, int b) {
return a > b ? a : b;
}
int lis(int arr[], int n, int *max_ref) {
if (n == 1) {
return 1;
}
int res, max_ending_here = 1;
for (int i = 1; i < n; i++) {
res = lis(arr, i, max_ref);
if (arr[i - 1] < arr[n - 1] && res + 1 > max_ending_here) {
max_ending_here = res + 1;
}
}
if (*max_ref < max_ending_here) {
*max_ref = max_ending_here;
}
return max_ending_here;
}
int main() {
int arr[] = {10, 22, 9, 33, 21, 50, 41, 60};
int n = sizeof(arr) / sizeof(arr[0]);
int max = 1;
lis(arr, n, &max);
printf("Length of LIS is %d\n", max);
return 0;
}
```
阅读全文