Topic:
Returns the and of the largest sub-array in an integer array.
Requirements:
Enter an array of shapes with positive and negative numbers in the array.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1],a[0] ... A[J-1] and the largest.
Returns the position of the largest subarray at the same time. The maximum value for the and of all sub-arrays.
Design ideas:
This is an iterative development, based on the previous task, in the previous program with a function, is to divide the ring array into n sub-arrays, each number of the ring array can be the first number of each array, and then in order to find out the maximum sub-array in each array results, The maximum value is calculated by comparison. The implementation of the ring array can be realized by using the data structure's Union table.
Code:
#include <iostream>using namespacestd;#defineN 100intShuzu (intHintArray2[n]) { inti; intH1; H1=h; intArray3[n]; array3[0]=array2[0]; intmaxsize=array3[0]; for(intE=0; e<=h-1; e++) {Array3[e]=Array2[e]; MaxSize=Array3[e]; for(i=e+1; i<=h1;i++) { if(array3[i-1]>0) {Array3[i]=array3[i-1]+Array2[i]; MaxSize=max (maxsize,array3[i-1]); } Else{Array3[i]=Array2[i]; }} ARRAY2[H1]=Array2[e]; H1=h1+1; } cout<<"the maximum number of sub-arrays is:"<<maxsize; cout<<"the array is:"<<Endl; return 0;}intMain () {inti,j; cout<<"Please enter the size of the ring array: (1-10)"<<Endl; CIN>>J; intArray1[n]; cout<<"Please enter"<<j<<"Number:"<<Endl; for(i=0; i<j;i++) {cin>>Array1[i]; } Shuzu (J,array1); return 0;}
Teammate Tanono Address: http://www.cnblogs.com/tyyhph/
Classroom Experiment 4.1 (ring array)