The maximum of the array continuous sub-vectors in C + + (Bull-Sword-point offer)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.