Class Practice (returns the number of the largest subarray in an integer array)

Source: Internet
Author: User
Tags sca

1. Design Ideas

It can be done with just one main function.

First, an integer array is defined, allowing the user to enter a set of integers.

Then, determine the number of positive and negative 0 of the situation.

Finally, choose the Execute if statement according to the judging situation, the case is all 0, negative number and 0, positive number and 0 positive negative number and 0 respectively.

2. Source code

1 /*2 * For the maximum of the subarray of an integer array3 * The start time 2016/4/8 16:504 * The end of time 2016/4/8 18:305 * Author Jing6 */7  PackageArraymax;8 ImportJava.util.*;9  Public classSumarray {Ten  One      Public Static voidMain (string[] args) { A          -Scanner sca=NewScanner (system.in); -System.out.println ("Number of input integer arrays"); the         intnum=sca.nextint (); -          -         inta[]=New int[num],b[]=New int[num]; -         inti; +System.out.println ("Enter an array of integers for this group"); -          for(i=0;i<num;i++) +         { Aa[i]=sca.nextint (); at         } -         intL=0,j=0,k=0,sum=0, Max; -          for(i=0;i<num;i++)//judging the positive and negative conditions of an input array -         { -             if(a[i]>=0) -             { inJ + +; -             } to             if(a[i]<0) +             { -k++; the             } *             if(a[i]==0) $             {Panax Notoginsengl++; -             } the         } +         if(K==num)//are all negative A         { theMax=a[0]; +              for(i=1;i<num;i++) -             { $                 if(max<A[i]) $                 { -max=A[i]; -                 } the             } -SYSTEM.OUT.PRINTLN ("Maximum sub-array and" +max);Wuyi         } the         Else if((l+k) ==num)//only negative numbers and 0 -         { WuSYSTEM.OUT.PRINTLN ("Maximum sub-array and 0"); -         } About         Else if(J==num)//are all non-negative $         { -              for(i=0;i<num;i++) -             { -sum+=A[i]; A             } +SYSTEM.OUT.PRINTLN ("Maximum sub-array and" +sum); the         } -         Else $         { the              for(i=0;i<num-1;i++) the             { the                 if(a[i]>=0&&a[i+1]>=0) the                 { -A[i+1]=a[i]+a[i+1]; inA[i]=0; the                 } the                 if(a[i]<0&&a[i+1]<0) About                 { theA[i+1]=a[i]+a[i+1]; theA[i]=0; the                 } +             } -K=0; theJ=0;Bayi              while(K<num)//only positive negative numbers in b[] after loop completion the             { the                 if(a[k]!=0) -                 { -b[j]=A[k]; theJ + +; the                 } thek++; the             } -             if(b[0]<0)//looking for the subscript of the first positive number the             { theI=1; the             }94             Else the             { theI=0; the             }98              while(i<j-2)//calculated only for positive and negative numbers About             { -                 if(B[i]> (-b[i+1]) && (-b[i+1]) <b[i+2])101                 {102B[i+2]=b[i]+b[i+1]+b[i+2];103B[i]=0;104B[i+1]=0; theI=i+2;106                 }107                 Else108                 {109I=i+2; the                 }111             } theMax=b[0];113              for(i=1;i<j;i++) the             { the                 if(max<B[i]) the                 {117max=B[i];118                 }119             } -SYSTEM.OUT.PRINTLN ("Maximum sub-array and" +max);121         }122     }123}
The Main Code

3. Results

4. Programming Summary

Due to the time problem, this program is a bit hasty, there are many shortcomings, and need to optimize the place. In my spare time, I continued to think about the process of making up a minimum of my own satisfaction.

Class Practice (returns the number of the largest subarray in an integer array)

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.