Maximum word group sum improvement!

Source: Internet
Author: User

 PackageCom.java.lianxi;ImportJava.util.*; Public classLianxi3 { Public Static voidMain (string[] args) {intNum,i,sum=0; Scanner Cin=NewScanner (system.in); System.out.print ("Please enter the length of the array:"); Num=Cin.nextint (); intarray[]=New int[num];  for(i=0;i<num;i++) {Array[i]=Cin.nextint (); }        intMax=array[0];  for(i=0;i<num;i++)        {             if(sum<=0) {sum=Array[i]; }             Else{sum=sum+Array[i]; }             if(sum>max) {Max=sum; }} System.out.println ("The maximum number of sub-arrays and is:" +max); }}
Max assigns an array to the first element
Sum is initially 0
Sum is then summed up, summed once, compared to Max once,
If the sum increment value is less than 0, then the increment is not continued, the current element is assigned to Sum,sum.
Until the end of the loop.
Max is the sum of the largest sub-arrays
Members: xueqing Lu Yu

Maximum word group sum improvement!

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.