First, title and requirements:
Title: Returns the maximum number of sub-arrays in an integer array and
Requirement (new addition): ① if array a[0] ... A[j-1] Next to the end, allow A[i-1] ... A[n-1],a[0] ... A[J-1] The sum of the largest; ② returns the position of the largest subarray at the same time.
Pair of people: Huyabao Gioyan
Second, design ideas:
We want to solve two problems: find the largest subarray in the ring array, and locate the sub-array position, the name of their output. To solve these two problems, we start from the following two aspects:
① to find the largest sub-array: The array values in order to be called "a trip", each to find the maximum number of sub-array, the first value to the last, into a new trip, and so on, until the completion of the traversal, to find the largest rings array.
② positioning sub-array: Determines the current and the value is negative, it is taken out, and the number of the next number of places to be stored as the largest sub-array head. When the maximum value is calculated, the position of the number is stored as the tail of the subarray. The last output will be the name of the sub-array.
Third, the source code:
1 //Pair Development--Huyabao Gioyan 2 3#include"stdafx.h" 4 5 6 int_tmain (intARGC, _tchar*argv[])7 { 8 inti,j,k,m,n,o,a[5]; 9 intsum,max,flag,flag1=0, Flag2,flag4;Ten Oneprintf"Please enter a 5 integer: \ n"); A for(k=0;k<5; k++) - { -scanf"%d",&a[k]); the } -Max = a[0]; - for(m=0;m<5; m++) - { + for(i=0;i<5; i++) - { +Sum =0; A for(j=i;j<5; j + +) at { -Sum =sum+A[j]; - if(sum<=0) - { -sum=0; -Flag1= (j+1+M)%5; in } - if(Sum >Max) to { +Max =Sum; -flag2=j+m; the } * } $ } Panax Notoginsengflag=a[0]; - for(n=0;n<5; n++) the { +a[n]=a[n+1]; A } thea[4]=Flag; + } - if(sum==0) $ { $ -max=a[0]; - for(intE=0;e<5; e++) the { - if(a[e]>=Max)Wuyi { themax=A[e]; -flag4=e; Wu } - } About $ } -printf"The rings of the maximum contiguous array is:%d\n", Max); -printf"The maximum contiguous rings array is:"); - A if(sum==0) + { theprintf"a[%d]", FLAG4); - } $ Else the { the intflag3=flag2-Flag1; the for(o=0; o<=flag3;o++) the { -printf"a[%d]\t", Flag1); inflag1++; the if(flag1>4) theflag1=0; About } the } theprintf"\ n"); the return 0; +}
Iv. Test and operation results:
Test data:
3 6-9 0 7 (positive, negative, 0)
7 9 3 2 8 (positive only)
-3-6-9-2-5 (negative only)
Operation Result:
Five, Experience:
The experiment we listened to the teacher's request, two people are each want to achieve the method, and then each of their own methods to speak to each other, the results of two people said to know their algorithm error in where, the shortcomings. Finally we decided to adopt the method of Zhang Shitong students.
Using the original code to make the maximum number of sub-arrays for each trip, use the For loop to put the first value to the last, to make a new trip, and so on, until the traversal is complete, to find the maximum rings array; But in the position we encountered difficulties, Huyabao and I in Saturday tried an afternoon to realize. There was a sense of accomplishment at the time, although the method was Huyabao, but it was particularly fulfilling.
Work Photo:
Software Engineering Classroom Training--knot pair Development loop array is the most Yamato