Returns the integer array of the largest subarray and

Source: Internet
Author: User

A Topic:

Returns the and of the largest sub-array in an integer array.

Requirements:

1. Enter an array of shapes with positive and negative numbers in the array.

2. One or more integers in the array make up a sub-array, each of which has a and.

3. Ask for the maximum value of all sub-arrays. Requires a time complexity of O (n).

Two Design ideas

1. Array num[] length has been determined to be N, the array of words in the arrays and put into the array sum[]

2.SUM[0]=NUM[0],SUM[1]=NUM[0]+NUM[1],SUM[2]=NUM[0]+NUM[1]+NUM[2]...

SUM[N]=NUM[1],SUM[N+1]=NUM[1]+NUM[2]... So loop until all your own and all of them are stored in the array sum .

3. then calculate The maximum value in the sum of the array and output the result.

Three Source

  

Sum. Cpp:defines the entry point for the console application.//#include "stdafx.h" #include "iostream.h" #define SIZE 5//number Number of Groups # # # MAXSIZE 100//The maximum number of subsets of a subset void caculatesum (int sum[],int length,int num[])//computes each subset in the array and {int j=0;for (int i=0;i <size;i++) {for (int k=i;k<size;k++) {if (i==k) {sum[j]+=num[k];} ELSE{SUM[J]=SUM[J-1]+NUM[K];} j + +;}}} The number of all cases int GETACC () {int acc=0;for (int i=1;i<=size;i++) {acc+=i;} return ACC;} Find the maximum value in the array, length is b[] in the number of int getmax (int b[],int length) {int max=b[0];for (int i=0;i<length;i++) {if (Max<b[i]) { Max=b[i];}} return Max;} int main () {int Num[size],sum[maxsize],max=0,acc;int count[2]={0,0};//num[num] is a required integer count[2] is the number of negative and positive numbers cout<< "Please enter" <<SIZE<< "integers:"; for (int i=0;i<size;i++)//calculate number of positive and negative numbers in array {cin>>num[i];if (num[i]<0) count[0]++;//number of negative numbers elsecount[1]++;//positive}ACC=GETACC (); for (i=0;i<acc;i++) {sum[i]=0;} if (count[0]==0)//If all positive words {for (int i=0;i<size;i++) {max+=num[i];}} else if ((count[1]==0) | | |         (count[0]==1)) If all is negative or there is a positive number {Max=getmax (num,size);} else//other conditions {caculatesum (sum,acc,num); Max=getmax (SUM,ACC);} cout<< "Maximum subset of the and is:" <<max<<endl;return 0;}

Four Operating Results and

  

  

  

  

  

  

Five The feelings of pair development

1. Oneself is responsible for writing the code, when oneself writes to the teammate, examines the mistake to use the time is relatively short, if if oneself examines the time to spend may be longer, because this is oneself writes, when you examine the time will unconsciously and at that time writes the code the thought coincident, may miss some logic error. And his partner can be an objective check.

Six. Work photos

2.

Returns the integer array of the largest subarray and

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.