A two-dimensional array to find the largest subarray

Source: Internet
Author: User

Two-dimensional array for maximal subarray and (ring)

First, the experimental topic

Returns the and of the largest sub-array in a two-dimensional array.

Experimental requirements:

      Enter a two-dimensional shape array with positive and negative numbers in the array.       two-dimensional arrays end-to-end, like a first-and-last tape. One or more integers in the       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). Second, the experimental ideas      this time we designed the experiment is manually input two-dimensional array of the number of rows and columns, two-dimensional array of ring sum the idea of our design is similar to an array, which is to put the first column of the finished array to the last, and so on. The maximum subarray is similar to the sum of the time and the two-dimensional array, i.e.: the input two-dimensional array is -1  2                                                                                3  4      

-5 9

(1) First calculate all the sub-arrays in the first row about-1, and then calculate the sub-array about 2, the same reason can be calculated in the second row and the third row of the word group.

(2) Put the sub-array of the above computed rows into an array of b[3][3] that is 1 1 2.

                                                                                             3  7   4                       

-5 4 9

(3) The maximum value of the subarray in b[3][3], this time should be in columns. First calculates the largest number of sub-arrays in the first column, calculates-1, then calculates the -1+3, calculates the -1+3+5, computes the maximum, then the maximum (-5) and Max comparison size, the largest, then (5) and Max size. In the same vein, the second to third column. You can get the maximum.

Third, the source program

//Letter 1201-2 class Si Sungong Wan Tong#include"stdafx.h"#include"iostream"using namespacestd;intMain () {intHang,lie; intA; inti,j; intS//sum    intcount; intMax; cout<<"Please enter the number of rows for the two-dimensional array:"; CIN>>Hang ; cout<<"Please enter the number of columns for the two-dimensional array:"; CIN>>lie; int**a;//defining dynamic two-dimensional arrays    int**B; A=lie*lie; A=New int*[Hang];  for(intk=0; k) {A[k]=New int[Lie]; } B=New int*[Hang];  for(intL=0; l) {B[l]=New int[A]; }    /*Array Initialization*/cout<<"Please enter an element in a two-dimensional array"<<Endl;  for(intI=0; i)    {         for(intj=0; j<lie;j++) {cin>>A[i][j]; }    }    /*two-dimensional array compression function*/     for(i=0; i) {Count=0;  for(j=0; j<lie;j++) {s=0;  for(intL=0; l<lie;l++)//each row is enumerated sequentially in a stored array{s=s+a[i][l+J]; B[i][count+l]=s; } Count=count+lie; A[i][lie+j]=A[i][j]; }} Max=b[0][0];//assigns the first number of arrays to Max     for(j=0; j<a;j++)    {         for(i=0; i) {s=0;  for(intR=0; r) {s=S+B[R+I][J];//enumerate by column array to find the maximum value                if(max<s) {max=s; } }}} cout<<"The sum of the largest sub-arrays is:"<<max<<Endl; return 0;}

Four. Experiments

Five. Experimental experience

I can only say that the teacher is more and more difficult for us, I and small partners to do a lot of attempts to succeed, in fact, if the last time to do the experiment is not lazy, the experiment does not need to do so big hands and feet, in fact, every experiment is a small change, but because the last experiment is not million essential oil, all this time It's a lesson to remember

A two-dimensional array to find the largest subarray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.