首页 > 代码库 > 最长上升子序列 C++源码

最长上升子序列 C++源码

#include <iostream>
using namespace std;
#define MAXSIZE 100

int max(int num1, int num2)
{
    return num1>num2?num1:num2;
}

int main()
{
    int num[MAXSIZE];
    int maxLen[MAXSIZE] = {1};
    int n;
    int i;
    int j;
    cin >> n;
    for (i=0; i<n; i++)
        cin >> num[i];
    for (i=1; i<n; i++)
        for (j=0; j<i-1; j++)
        {
            if (num[i] > num[j])
                maxLen[i] = max(maxLen[j], maxLen[i]+1);
            else
                maxLen[i] = max(maxLen[i], maxLen[j]);
        }
    cout << maxLen[n-1] << endl;
    return 0;
}

 

最长上升子序列 C++源码