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, and then all the sub-arrays are calculated and 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:
1 //Pair Development--Huyabao Gioyan2#include"stdafx.h"3 4 int_tmain (intARGC, _tchar*argv[])5 {6 7 //The first of these methods8 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 to /*int i,j,a[5]; + printf ("Please enter 5 integers: \ n"); - For (j=0;j<5;j++) the { * scanf ("%d", &a[j]); $ }Panax Notoginseng int Max = 0; - int Sum = 0; the For (i=0;i<5;i++) + { A Sum =sum + a[i]; the if (Sum > Max) + Max = Sum; 3 - $ if (Sum < 0)//if and less than 0, start the accumulation again from the next element $ Sum = 0; - } - printf ("The%d\n of the maximal contiguous Subarray is:", max);*/ the - Wuyi return 0; the}
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:
1, Division of labor and cooperation: I and Gioyan from the first method to the second method are thinking together, including two methods of implementation and improvement methods. I am primarily responsible for code programming after determining the method, and Gioyan is primarily responsible for code testing.
2, in the process of writing code, because we think well in advance of ideas, so programming is relatively simple. But in the process of writing there are still a few problems. 1 when the For loop has two layers nested and the layer is affected by the outer layer, the condition of the second loop depends on the first layer, and I don't think about it. 2 at first I set the initial value of both the value and the maximum to 0, and later found that it should be the first element of the array. 3 At first I set the number of arrays worth to 3, and then found that the number is too small, affecting the diversity of testing, can not fully test the situation, and then set to 5.
Software Engineering class Assignment (IV.)--pair development