java-Arithmetic-Four

Source: Internet
Author: User

Title: Find arrays connected to annular and largest contiguous sub-arrays

Experiment Code:

1  PackageZuoye;2 3 ImportJava.util.Scanner;4 5 6  Public classMaxsubarray {7      Public Static voidMain (string[] args) {8Scanner scan =NewScanner (system.in);9         Ten          One         //input Data ASystem.out.println ("Please enter array length"); -         intn =scan.nextint (); -         int[] A =New int[n]; the          -System.out.println ("Please enter array elements"); -          for(inti = 0;i < n;i++) -         { +A[i] =scan.nextint (); -         } + scan.close (); A         //computes the and largest contiguous subarray of this array at         int[] result =maxsub (a,a.length); -SYSTEM.OUT.PRINTLN ("non-connected and largest contiguous subarray of loops:"); -          for(inti = Result[0];i <= result[1];i++) -         { -System.out.print (A[i] + "\ T"); -         } inSystem.out.println ("and for:" + result[2]); -      to      +          -          the         //The array is connected to a ring, and then the largest contiguous subarray of this array is computed. *         //Connect to a ring the array is then connected, but the last element of the array is not connected, which is equivalent to the sum of the largest subarray of the array after the connection is computed . $         int[] B =New int[2 * n-1];Panax Notoginseng          for(inti = 0;i < n-1;i++) -         { theB[i] =A[i]; +B[n + i] =A[i]; A         } theB[n-1] = a[n-1]; +         int[] Result2 =maxsub (b,n); -System.out.println ("\ n \ nthe array is connected to the largest contiguous subarray after the loop):"); $          for(inti = Result2[0];i <= result2[1];i++) $         { -System.out.print (B[i] + "\ T"); -         } theSystem.out.println ("and for:" + result2[2]); - Wuyi          the     } -      Wu      -      About      $     //computes the and largest contiguous subarray of a array (a array is an equivalent array after a loop, that is, twice times the original array, and n is the length of the original array) -      Public Static int[] Maxsub (int[] A,intN) -     { -         intan = A.length;//The length of an equivalent array of rings A         intCurrectsum = a[0];//record current cumulative sum, initial value is a[0] +         intCurrectstartindex = 0;//record the starting subscript for the current summation, with an initial value of 0 the         intCount = 1;//record the number of accumulated elements with an initial value of 1 -         int[] result =New int[3];//records the information of the result Subarray, $Result[0] = 0;//start subscript for result Sub-array theRESULT[1] = 0;//end subscript for result Sub-array theRESULT[2] = a[0];//the and of the result sub-array the          for(inti = 1;i < an;i++)//iterate through each element of the a array in turn the         { -             if(currectsum <= 0)//if the current sum is not greater than 0, no more than 0 for the subsequent elements do not contribute, can be removed, so should be a[i] place to restart the add in             { theCurrectsum = A[i];//the current summation and assignment is a[i] theCurrectstartindex = i;//assigns the starting subscript of the current cumulative value to I AboutCount = 1;//Count the number of accumulated elements as 1 the             } the             Else//current cumulative and greater than 0, continue to add a[i] the             { +Currectsum + =A[i]; -count++;//the number of current add-on elements plus one the             }Bayi             if(Currectsum > Result[2])//If the current sum and is greater than the sum and result[2 of the original result array, the result subarray information should be updated to the current subarray, because the sum of the current sub-array and greater than the result sub- array the             { theResult[0] = Currectstartindex;//The start subscript of the result sub-array is the start subscript of the current sub-array -RESULT[1] = i;//the end subscript of the result sub-array is assigned the value I -RESULT[2] = currectsum;//The summation and assignment of the result sub-array is the sum of the current Subarray and the             } the             if(Count >= N)//if the sum of the number of elements equals the length of the original array (an array not connected to the ring), then the most added element is not added, that is, the largest sub-array is the original array, should end the loop the             { the                  Break; -             } the         } the         returnresult; the     }94      the      the      the}

Experiment:

java-Arithmetic-Four

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.