//求最长单调递增子序列 #include<stdio.h> #define MAXN 20 void disp(int a[],int b[],int k){ int i; for(i=k-1;i>0;i--){ if(b[k] == b[i]+1 && a[i] <= a[k]){ disp(a,b,i); break; } } printf("%d ",a[k]); } int maxL(int b[],int n){ //求数组b中最大值下标 k int i,k=0; int max = b[0]; for(i = 0;i<n;i++){ if(max<b[i]){ max = b[i]; k = i; } } return k; } void Maxsubseq(int a[],int b[],int n){ int i,j,k; b[0] = 1; for(i=1;i<=n;i++){ k = 0; for(j=0;j<i;j++) if(a[j] <= a[i] && k < b[j]) k = b[j]; b[i] = k+1; } } int main(){ int a[] = {4,5,1,3,6,7,2,9,8}; int b[MAXN]; int n = 8,k; Maxsubseq(a,b,n); k = maxL(b,n); printf("最长单调递增子序列最后元素下标k=%d\n",k); printf("最长单调递增子序列:"); disp(a,b,k); printf("\n"); return 0; }
原文地址:https://www.cnblogs.com/Hqx-curiosity/p/12153112.html
时间: 2024-10-14 23:53:02