The and of the maximal subarray of the one-dimensional loop array

Source: Internet
Author: User

Pairs of Members: letter 1201-1 class at the ocean Guampeppe

I. Topics and Requirements

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

Requirements: Enter an array of shapes, with positive numbers in the array and negative values. 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.

Two. Design ideas

Take advantage of the previous return of an integer array of maximum subarray and the idea of the program, but here the number is placed in a list, end to end, to find the sum of the maximum subarray.

Three. Source code

//one-dimensional array. Cpp:defines the entry point for the console application.//#include"stdafx.h"#include<iostream.h>#defineNum 5/*linked list data structure*/typedefstructlnode{intdata; structLnode *Next;} Lnode,*linklist;/*the initialization of a linked list*/voidInitlist (Linklist &L) {L=NewLnode; L->next=NULL;}/*insertion of linked list data*/voidInsertlist (linklist &l)//set up a circular link list{Lnode*head,*temp; Head=L; cout<<"Please enter"<<num<<"Number of:";  for(intI=0; i<num;i++) {Temp=NewLnode; CIN>>temp->data; Temp->next=NULL; Head->next=temp; Head=head->Next; } head->next=l->next;//build a circular chain list}voidoutput (linklist L) { for(intI=0; i<num;i++) {cout<<L->next->data<<" "; L=l->Next; }}intMain () {intmax,sum,flag=0;//Sums are the sum of the word groups, and Max is the largest sub-array and    intordern=0, orderx=0;    Linklist L; Lnode*temp,*temp1,*temp2,*Head;    Initlist (L);                    Insertlist (L); //inserting data into a linked list by the userTemp=l->Next; Max=l->next->data;//Max Initial value is the first number in a linked list     for(intI=0;i<num;i++,temp=temp->next) {Temp2=temp;  for(intj=0;j<num;j++,temp2=temp2->next) {             for(intk=j;k<num;k++) {sum=0; Temp1=Temp2;  for(intH=j;h<=k;h++,temp1=temp1->next) {Sum=sum+temp1->data; }                 if(max<sum)//assigns the maximum value to Max, and saves the current ordinal number{Max=sum; Ordern=J; Orderx=K; Head=temp; Flag=i;//The case of a linked list when used to find the maximum value.} }}} Temp=l->Next; cout<<"the maximum number of Word groups is:";  for(i=0;i< (Flag+ordern); i++)//find the first number of sub-arrays when the maximum value is obtained{Temp=temp->Next; }     for(intj=0;j< (orderx-ordern+1); j++,temp=temp->next)//will get maximum and sub-array element output{cout<<temp->data<<"  "; } cout<<endl<<"the number of the largest sub-arrays is:"<<max<<Endl;; return 0;}

Four. Results and

Five experiences

When we say our own ideas, we find that there are some places to complement each other, the integration is better than our own, the code is concise, in the debugging, there are some errors, but we say that the mistake we found, the efficiency is very high.

The and of the maximal subarray of the one-dimensional loop array

Related Article

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.