Software Engineering Personal Assignment 05

Source: Internet
Author: User

Design ideas:

An array to find and the largest sub-array, I think it is to look for a can produce and the largest sub-array of an array of the largest arrangement, that is, to remove the smallest negative number. If the array is positive, all the numbers add up and the maximum, if there are negative numbers in the array, the smallest negative number is removed, and the remaining sub-array is found in one and the largest sub-array. The method of removing the minimum negative number is to iterate over the array, find the minimum value, place it at the top of the array or the bottom.

Source:

ImportJava.util.*; Public classArrayMax { Public Static voidMain (String args[]) {Scanner input=NewScanner (system.in); System.out.print ("Please enter the length of the array:"); intn =Input.nextint (); intData[] =New int[n]; intArray[] =New int[n]; System.out.print ("Please enter an array:");  for(inti=0;i<n;i++) {Data[i]=Input.nextint (); }                intsum = 0; intMin = data[0]; intNumber = 0;  for(inti=0;i<n;i++)        {            if(min>Data[i]) {min=Data[i]; number=i; }        }         for(inti=number;i<n;i++) {Array[i-number] =Data[i]; }         for(inti=0;i<number;i++) {Array[i+n-number] =Data[i]; }                intMax =min;  for(inti=0;i<n;i++) {sum= Sum +Array[i]; if(sum>max) {Max=sum; }                        if(sum<0) {sum= 0; }} System.out.println ("The maximum value of all sub-arrays is:" +max); }}
View Code

Results:

Software Engineering Personal Assignment 05

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.