First, title and requirements:
1. Title: Returns the and of the largest subarray in an integer array.
2. Requirements:
① input An array of shapes, there are positive and negative numbers in the array, one or more consecutive integers in the array make up a sub-array, each sub-array has one sum, and the maximum value of the sum of all the sub-arrays, and the required time complexity is O (n).
② two pairs to complete the programming task; One person is responsible for program analysis, code programming, one person responsible for code review and Code test plan.
3, pair of personnel: Huyabao Gioyan
Second, design ideas:
① The first method, defines an array, lists all the sub-arrays, and then the and all of them are put into the array, then the maximum value, output. But in the process of writing, we found that if the loop way to store, then the serial number is not easy to row, so we think of edge-to-edge ratio, starting from the first number, to find all the array containing it, summed in order, the maximum value is stored in max. This does implement the required functionality, but the time complexity is O (n*n) and does not meet the requirements.
② the second method, we found that if from the first number to add, add to a value, when the and is negative, no matter what the next value, can not be the largest, then discard the previous and the value, directly from the beginning of the addition, is still the edge plus edge ratio, until the maximum value, there is only a for loop at this time, The event complexity is O (n).
Third, the source code:
#include"stdafx.h" 3 4 int_tmain (intARGC, _tchar*argv[])5 { 6 7 //The first of these methods 8 inti,j,k,a[5]; 9 intSum,max;TenMax = a[0]; Oneprintf"Please enter a 5 integer: \ n"); A for(k=0;k<5; k++) - { -scanf"%d",&a[k]); the } - - for(i=0;i<5; i++) - { +Sum =0; - + for(j=i;j<5; j + +) A { atSum =sum+A[j]; - if(Sum >Max) -Max =Sum; - } - } -printf"the number of the largest contiguous subarray is:%d\n", Max); in
//The second method of inti,j,a[5]; printf ("Please enter a 5 integer: \ n"); for(j=0;j<5; j + +) {scanf ("%d",&A[j]); } intMax =0; intSum =0; for(i=0;i<5; i++) {Sum=sum +A[i]; if(Sum >Max) Max= Sum;3 if(Sum <0)//if and less than 0, the accumulation starts again from the next elementSum =0; } printf ("the number of the largest contiguous subarray is:%d\n", Max); */return 0; }
Test data:
-3-6-1-8-7 (all negative)
5 3-9 0 9 (positive, negative, 0 in array)
6 9-5 1 6 (there is a positive negative in the array, but the maximum sub-array does not contain negative numbers)
4-2 3-6 8 (This maximum sub-array includes negative numbers)
Four, Experience:
Because the Huyabao Foundation is better than mine, she is primarily responsible for code programming after determining the method, and I am primarily responsible for testing the code. But the idea is to rely on two people to discuss the common only appeared. I work with Huyabao to find that she has a lot of advantages worthy of my study, she is more love hands, have any idea of love in the paper painting, Ben on the knock. I always think it's wrong. But two of people work together to complement each other. The whole process was easy and enjoyable.
At the time of testing, the code for Huyabao is correct and complete. We used the method taught by the teacher in the last lesson to test it. The first group is all negative, the second group has only one negative number, the negative number is in the middle, and the third group is positive and negative. The results of the three sets of tests are consistent with the predictions.
Software Engineering Classroom Training--pair development