Pair development returns the sum of the largest sub-array in an integer array

Source: Internet
Author: User

First, the topic requirements

Title: Returns the and of the largest sub-array in an integer array.

Requirements: Enter an array of shapes, with positive numbers in the array and negative values. One or more consecutive integers in an array make up a sub-array, each of which has a and.

If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest. Returns the position of the largest subarray at the same time.

The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Pair programming Requirements: The pair completes the programming task.

One person is mainly responsible for program analysis, code programming. One person is responsible for code review and Code test plan.

Publish a blog post about design ideas, problems that arise, possible solutions (multiple choices), source code, results, summaries.

(Until 18:00 on April 15)

Second, design ideas

This cooperative design idea is very simple to understand, that is, the first element of the array is the first element of the word group, then add, the second round to the second element of the array is the first element of the word group, and then add, the original array of the first element is set to the last element. And so on, that is, each time the last element of the array is treated as the first element of the array, the loop array is formed.

Third, the Code

#include <iostream>#include <time.h>#define N 5UsingNamespaceStdvoidMain () {int a[n],i,j,w=0,b[n][n],p1=0,p2=0, T,m; Srand ((int) Time (0));//The element value of an array is generated by a random functionfor (i=0;i<n;i++) {A[i]=-rand ()%36+25; }//The element value of the output arrayfor (i=0;i<n;i++) {cout<<a[i]<<Endl }//To find a sub-arrayfor (i=0;i<n;i++) {m=I w=0; j=0;while (j<=n-1) {w+=A[M]; b[i][j]=W m++;if (m>n-1) {m=0; } J + +; }} t=b[0][0];for (i=0;i<n;i++) {for (j=0;j<n;j++) {if (b[i][j]>T) {t=B[I][J]; p1=I P2=J }}} cout<< "" <<t <<endl; Cout<< "  "<<0while (I<=p2) {Cout<<p1<< "" if (P1>=n) {p1=0< Span style= "color: #000000;" >; } i++endl;}      

Iv. Results of program operation

V. Summary

This procedure is relatively simple to write, because there is a previous one-dimensional array of the maximum value of the program, so there are some changes in that program, the changes are not too large. In the future to solve the more difficult problems, will try to put the big problem into one of the small problems in turn to solve. Like this ring array, it may be laborious to write an array of rings at the beginning, but it is not so difficult to write on the basis of a previous one-dimensional array. When writing more complicated programs, the design idea should be from small to large, and the programming process should be small.

Vi. Work Photos

Pair development returns the sum of the largest sub-array in an integer array

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.