Pair development ⅴ--Loop one-dimensional array with the largest sum of sub-arrays

Source: Internet
Author: User

First, design ideas

(1) The storage structure of the data is the linked list, and the next point of the last node points to the node of the first element;

(2) The number of data is n, there is a maximum of n (n+ (n-1) +...+1) case (including repetition);

(3) The remainder is similar to the two-dimensional array.

Second, the source code

1 //one-dimensional array. Cpp:defines the entry point for the console application.2 //Guampeppe in the ocean3 4#include"stdafx.h"5#include <iostream.h>6 #defineNum 57 /*linked list data structure*/8typedefstructLnode9 {Ten     intdata; One     structLnode *Next; A}lnode,*linklist; - /*the initialization of a linked list*/ - voidInitlist (Linklist &L) the { -L=NewLnode; -l->next=NULL; - } + /*insertion of linked list data*/ - voidInsertlist (linklist &l)//set up a circular link list + { ALnode *head,*temp; atHead=L; -cout<<"Please enter"<<num<<"Number of:"; -      for(intI=0; i<num;i++) -     { -temp=NewLnode; -Cin>>temp->data; intemp->next=NULL; -head->next=temp; toHead=head->Next; +     } -head->next=l->next;//build a circular chain list the } * voidoutput (linklist L) $ {Panax Notoginseng      for(intI=0; i<num;i++) -     { thecout<<l->next->data<<" "; +L=l->Next; A     } the } + 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; the initlist (L); -Insertlist (L);//inserting data into a linked list by the userWuyiTemp=l->Next; themax=l->next->data;//Max Initial value is the first number in a linked list -      for(intI=0;i<num;i++,temp=temp->next) Wu     { -Temp2=temp; About          for(intj=0;j<num;j++,temp2=temp2->next) $         { -              for(intk=j;k<num;k++) -             { -sum=0; Atemp1=Temp2; +                  for(intH=j;h<=k;h++,temp1=temp1->next) the                 { -Sum=sum+temp1->data; $                 } the                  if(max<sum)//assigns the maximum value to Max, and saves the current ordinal number the                  { themax=sum; theordern=J; -orderx=K; inHead=temp; theFlag=i;//The case of a linked list when used to find the maximum value. the                  } About             } the         } the     } theTemp=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 the     {BayiTemp=temp->Next; the     } the      for(intj=0;j< (orderx-ordern+1); j++,temp=temp->next)//will get maximum and sub-array element output -     { -cout<<temp->data<<"  "; the     } thecout<<endl<<"the number of the largest sub-arrays is:"<<max<<Endl;; the  the     return 0; -}

Third, the operation

Iv. Experience

The biggest feeling is that two of us have their own ideas, all want to explain their thoughts to each other, resulting in the inability to listen to each other's voice. I think his method is a waste of memory space and he thinks his search process is clear. Finally, the students in the ocean compromise, using my ideas, with a circular list. The development process is similar to the last two times, individuals have personal advantages and disadvantages, can compensate each other. When we debugged the program, we worked together to troubleshoot the error.

Five, no picture no truth

Pair development ⅴ--Loop one-dimensional array with the largest sum of sub-arrays

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.