Classroom test for maximum word count and

Source: Internet
Author: User

First, title:
An array of integers with a positive negative integer, in which one or more integers constitute an integer group, with one and, for the maximum subarray, required, time complexity O (n)

Second, design ideas:

1, first enter the number of elements to be entered.

2, build two int type Word group, one to hold the number, one as a temporary array.

3, meet a positive number is stored in the temporary array, and continue to add, as long as the result is just continue to store. Until a negative number is met, truncation is processed.

4. Sort the number in the temporary array (without bubbling, taking into account O (n)), output.

Three. Source

 PackageShuzuadd;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsScanner in =NewScanner (system.in); intN//Shuruchangdun=In.nextint (); intarray1[]=New int[n]; intarray2[]=New int[N]; intSum,geshu=0; intMax = 0;  for(inti=0;i<n;i++) {Array1[i]=In.nextint (); } Sum=0;  for(ints=0;s<n;s++)      {                         if(sum<0) {sum=array1[s+1]; }               if(sum>=0) {sum=sum+Array1[s]; Array2[geshu]=sum; Geshu++; }                       }        for(intx=0;x<=geshu;x++)       {               if(array2[x]<array2[x+1]) Max=array2[x+1]; if(array2[x]>=array2[x+1]) array2[x+1]=Array2[x];    } System.out.println (max); }}

Four. Run:

Classroom test for maximum word count and

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.