8 389 207 155 300 299 170 158 65 #include <iostream> using namespace std; #define len(a) (sizeof(a) / sizeof(a[0])) //数组长度 int lis(int arr[], int len) { int longest[len]; for (int i=0; i<len; i++) longest[i] = 1; for (int j=1; j<len; j++) { for (int i=0; i<j; i++) { if (arr[j]>arr[i] && longest[j]<longest[i]+1)//if(arr[j]>arr[i]){ //注意longest[j]<longest[i]+1这个条件,不能省略。 longest[j] = longest[i] + 1; //计算以arr[j]结尾的序列的最长递增子序列长度 } } } int max = 0; for (int j=0; j<len; j++) { //cout << "longest[" << j << "]=" << longest[j] << endl; if (longest[j] > max) max = longest[j]; //从longest[j]中找出最大值 } return max; } int main() { int arr[80] ; //测试数组 int n;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&arr[i]); int ret = lis(arr,n); //cout << "max increment substring len=" << ret << endl; cout<<ret;return 0; }
新闻热点
疑难解答