新浪多媒体题目求解
今天新浪多媒体的一个题说:找出给定数组的单调递增子序列的最大长度,该子序列可以不连续。 我敲了以下代码,不知道为什么通过率0%。有做了一样的题的大牛解答下吗? 输入要求:n a1 a2 a3 ... an (n为数组长度,a1-an为数组元素) 输出要求:单调递增子列最大长度#include <iostream> #include <stdlib.h> #include<stdio.h> using namespace std;
int main() { int n; int i = 0,j; char c; int length = 0; int maxlen = 0; cin >> n; int* a = (int*)malloc(n * sizeof(int)); for (i = 0; i < n; i++) { cin >> a[i]; } int previous; for (i = 0; i < n; i++) { previous = a[i]; //cout << "index" << previous << endl; for (j = i+1; j < n; j++) { if (a[j] > previous) { length++; previous = a[j]; //cout << length << " " << a[j] << endl; } } if (length > maxlen) maxlen = length+1; length = 0; //cout << " " << endl; } cout << maxlen << endl; free(a); //cin >> n; return 0; }
#新浪##笔试题目##题解#