The sword refers to the offer series source code-the maximum sub-vector and (the maximum and the contiguous subarray)

Source: Internet
Author: User

Topic 1372: Maximum sub-vectors and (maximum and duration of contiguous subarray) time limit: 1 seconds Memory limit: 32 Mega Special: No submission: 2169 Resolution: 570 Topic Description: Hz occasionally take some professional questions to confuse those non-computer majors. Today JOBDU Test team after the meeting, he said again: in the Ancient one-dimensional pattern recognition, it is often necessary to calculate the maximum sum of continuous sub-vectors, when the vector is all positive, the problem is well solved. 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? Input: Input has multiple sets of data, each set of test data consists of two rows. The first behavior is an integer n (0<=n<=100000), and when n=0, the input ends. The next line contains n integers (we guarantee that all integers belong to [ -1000,1000]). Output: For each test case, you need to output a single line of 3 integers representing the largest of the successive sub-vectors, the subscript of the first element of the sub-vector, and the subscript of the last element. If there are multiple sub-vectors, the one with the lowest subscript of the starting element is output. Sample input: 3-1 -3-25-8 3 2 0 586-3-2 7-15 1 2 20 Sample output:-1 0 010 1 48 0 3

#include <iostream> #include <stdio.h>using namespace Std;bool isinvalid = false;//Maximum sub-array and int Findgreatestsumofsubarray (int* data,int length,int& left,int& right) {if (data==null| |        length<0) {isinvalid = true;    return 0;    } Isinvalid = false;    int cursum=0;            int temp =0;//record start position int ngreatestsum = 0x80000000;//Initial integer minimum number for (int i=0;i<length;i++) {if (cursum<0) {            temp = i;        Cursum = Data[i];        }else{Cursum+=data[i];            } if (cursum>ngreatestsum) {ngreatestsum = Cursum;        left = temp;//start position updated right = i; }} return ngreatestsum;}    int main () {int n;        while (scanf ("%d", &n)!=eof&&n) {int* data = new Int[n];        for (int i=0;i<n;i++) {scanf ("%d", &data[i]);        } int left=0,right=0;        int max = Findgreatestsumofsubarray (data,n,left,right);   printf ("%d%d%d\n", max,left,right); } return 0;} 

The sword refers to the offer series source code-the maximum sub-vector and (the maximum and the contiguous subarray)

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.