One-dimensional array summation 2

Source: Internet
Author: User

#include <iostream>using namespacestd;#defineNUM 100/*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=m;  for(intI=0; i<num;i++) {Temp=NewLnode; Temp->data=rand ()%2000000000-100000000; Temp->next=NULL; Head->next=temp; Head=head->Next; }}voidoutput (linklist L) { for(intI=0; i<num;i++) {cout<<L->next->data<<" "; L=l->Next; }}intMainintargcChar*argv[]) {    intmax,sum,flag=0;//Sums are the sum of the sub-arrays, and Max is the sum of the largest sub-arrays    intordern=0, orderx=0;    Linklist L; Lnode*temp1,*Temp2;    Initlist (L);                    Insertlist (L); //inserting data into a linked list by the userTemp2=l->Next; Max=l->next->data;//Max Initial value is the first number in a linked list     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; } }} Temp1=l->Next; cout<<"array:"<<Endl;    Output (L); cout<<endl<<"the maximum number of sub-arrays is:";  for(intI=0; i<ordern;i++)//find the first number of sub-arrays when the maximum value is obtained{Temp1=temp1->Next; }     for(intj=0;j< (orderx-ordern+1); j++,temp1=temp1->next)//will get maximum and sub-array element output{cout<<temp1->data<<"  "; } cout<<endl<<"the number of the largest sub-arrays is:"<<max<<Endl;; return 0;}

One-dimensional array summation 2

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.