First, the title: Returns the number of the largest subarray in a two-dimensional integer array.
Second, requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
A two-dimensional array is connected to the end of the line, like a belt.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).
Third, the design idea
This experiment is based on the previous experiments, using dynamic arrays, in order to satisfy the end-to-side connection, so after the calculation once, the number is placed on the last edge of the array, so that the loop traversal finally find the sum of the largest sub-array.
Iv. Source Code
1#include <iostream.h>2 intMaxsubarray (int**a,intNintm)3 {4 int**p=New int*[n];5 inti,j;6 if(m==0|| n==0)7 return 0;8 //calculation P[i][j]9 for(i=0; i<n;i++)Ten { Onep[i]=New int[m]; A for(j=0; j<m;j++) - { - if(i==0) the { - if(j==0) -p[i][j]=A[i][j]; - Else +p[i][j]=p[i][j-1]+A[i][j]; - } + Else A { at if(j==0) -p[i][j]=p[i-1][j]+A[i][j]; - Else -p[i][j]=p[i][j-1]+p[i-1][j]-p[i-1][j-1]+A[i][j]; - } - } in } - //computes the and of the largest subarray of two-dimensional arrays to inttemp; + intmax=a[0][0];//Initialize - intsum; the if(m==1) * { $ for(i=0; i<n;i++)Panax Notoginseng { - for(j=i;j<n;j++) the { + if(i==0) A { thetemp=p[j][m-1]; + } - Else $ { $temp=p[j][m-1]-p[i-1][m-1]; - } - if(sum<temp) thesum=temp; - }Wuyi } the } - Else Wu { - for(i=0; i<n;i++) About { $ for(j=i;j<n;j++) - { - if(i==0) - { Atemp=p[j][m-1]-p[j][m-2]; + } the Else - { $temp=p[j][m-1]-p[j][m-2]-p[i-1][m-1]+p[i-1][m-2]; the } the for(intk=m-2; k>=0; k--) the { the if(temp<0) -temp=0; in if(i==0) the { the if(k==0) Abouttemp+=P[j][k]; the Else thetemp+=p[j][k]-p[j][k-1]; the } + Else - { the if(k==0)Bayitemp+=p[j][k]-p[i-1][k]; the Else thetemp+=p[j][k]-p[j][k-1]-p[i-1][k]+p[i-1][k-1]; - } - if(sum<temp) thesum=temp; the } the } the } - } the returnsum; the } the 94 intMain () the { the intN//Number of rows the intM//Number of columns98 intSum//the and of the largest sub-array About inti,j; -cout<<"Please enter the number of rows for the two-dimensional array:"<<Endl;101Cin>>N;102cout<<"Please enter the number of columns for the two-dimensional array"<<Endl;103Cin>>m;104 the int**a=New int*[n];106cout<<"Please enter this two-dimensional array element:"<<Endl;107 for(i=0; i<n;i++)108 {109a[i]=New int[m]; the for(j=0; j<m;j++)111 { theCin>>A[i][j];113 } the } the thesum=Maxsubarray (a,n,m);117cout<<"the sum of the largest sub-arrays of two-dimensional arrays:"<<sum<<Endl;118 return 0;119}
V. Summary of the Experiment
This experiment, we exchanged roles, I am responsible for code writing, program analysis, Tan Wei responsible for code review and Code test plan, experience the role of the different feelings after the exchange, the experiment we changed many times, the variable reuse inappropriate, sometimes different meaning variable we use the same variable expression, some confusion, In the future when programming should consider the meaning of the good variables, so as to see the name of understanding righteousness.
VI. Cooperation Photos
5, software engineering knot pair development The sum of the continuous maximal sub-arrays in a one-dimensional array