Hertz occasionally takes some professional questions to confuse students who are not computer majors. Today, the test team after the meeting, he said again: in the Ancient one-dimensional pattern recognition,//often need to calculate the maximal sum of continuous sub-vectors, when the vector is all positive, the problem is very good solution. However, if the vector contains a negative number, should it contain a negative number and expect that the next positive number will compensate for it? For example: {6,-3,-2,7,-15,1,2,2}, the maximum and 8 of continuous sub-vectors (starting from No. 0 to 3rd). Will you be fooled by him? #include <iostream>using namespace std; int grial (int a[],int n) {int i = 0;int b[n*n];int sum= 0; int k = 0;for (; i<n;i++) {sum=a[i];b[k++]=sum;for (int j = I+1;j <n;j++) {sum+=a[j];b[k++]=sum;}} int Temp;for (i=0;i<1;i++) {for (int j=i+1;j<k;j++) {if (B[i]<b[j]) {temp = B[i];b[i] = b[j];b[j] = temp;}}} return b[0];} int main () {int a[]={-1,2,3,4,2,-2};cout<<grial (a,6) <<endl;return 0;}Thought: More than an array to save the value, I feel not very good, but in the Ox-Guest sword is the perfect way to pass.
The maximum of the array continuous sub-vectors in C + + (Bull-Sword-point offer)