The task of this experiment:
Thinking Analysis:
The two dimensions are split into one dimension, the sum of the maximal sub-arrays of each row is obtained, and the starting and terminating elements of the maximal subarray of each row are evaluated, compared with other rows, check whether Unicom, if Unicom, will be two rows of sub-array maximum value and add, find out the maximum value of the maximum value of different row sub-arrays
Implemented code:
1#include <iostream>2#include <ctime>3 intMax_ (intJint*array,int&first,int&Last );4 using namespacestd;5 #defineRow 5//Number of rows6 #defineCol 5//Number of columns7 intMain ()8 {9Srand ((int) time (NULL));//Random SeedsTen intI, j,a[ -][ -]; One intarray[ -]; A intmax_row[ -]; - intsum=0, max=0; - intfirst_num=0, last_num=0; the for(i =0; i < row; i++) - { - for(j =0; J < Col; J + +) - { + //assign a value to a two-dimensional array from a random number from (-100) to 100 -A[I][J] = rand ()% ( --(- -)+1)- - ; + } A } atcout <<"the array is:"<<Endl; - //Display Array - for(i =0; i < row; i++) - { - for(j =0; J < Col; J + +) - { incout << a[i][j]<<" "; - } tocout <<Endl; + } -cout <<Endl; the //------------------------------------------------------------ * //each row is given a one-dimensional array $ for(i =0; i < row; i++)//each rowPanax Notoginseng { - for(j =0; J < Col; J + +)//each column the { +ARRAY[J] =A[i][j]; A } theMax_row[i]=max_ (J, Array,first_num,last_num);//The maximum number of sub-arrays and values for each row +cout << Max_row[i] <<" "<< first_num<<" "<<last_num<<Endl; - $ } $ return 0; - } - //find the maximum value of a one-dimensional array sub-array the intMax_ (intJint*array,int&first,int&Last ) - { Wuyi intsum =0, max =0; the for(j =0; J < Col; J + +) - { Wusum = sum +Array[j]; - if(Sum >max) About { $Max =sum; - } - if(Sum <0) - { Asum =0; + } the if(max = =0) - { $sum = sum +Array[j]; the if(Sum >max) the { theMax =sum; the } - } in the } the returnMax; About}
Effect:
Experience: Because the main focus on the arithmetic Android (and not too much results), resulting in this task time a little tense, think enough, now only achieve the maximum value of each row of sub-arrays, encountered bottlenecks: Because of the reason for the initial element of the algorithm and the termination of the element is a bit confusing, tomorrow refueling, today's head is a bit messy , learn something else first.
Unicom Sub-array (i)