Returns the sum (with rings) and integer arrays of the largest sub-array in an integer array.

Source: Internet
Author: User
Tags sca

Returns the sum (with rings) and integer arrays of the largest sub-array in an integer array.

1. Design Philosophy

(1) create a one-dimensional array a [] to store data based on the length and content of the array entered by the user.

(2) then several variables are defined. sum is used for summation, max is and maximum, and num is the array length.

(3) Start the for loop. sum is initialized to 0, and max is initialized to a [0]. The loop content is sum + = a [I]. If sum is greater than max, the sum value is assigned to max. If sum is smaller than 0, the sum = 0 is defined. Until the loop ends, get the sum of the largest sub-array.

(4) Find the minimum value of array a, obtain its subscript j, and assign the j value to t. Create array B and perform two cycles. For the first loop, I = 0. The j value increases by 1 for each loop. B [I] = a [j], when j> = num, the first loop ends. For the second loop, make sum = 0, j = 0. The j value of each loop increases by 1 and B [I] = a [j]. When j> = t, the loop ends.

(5) Start the for loop. sum is initialized to 0, and max is the maximum value obtained before the ring is completed. The loop content is sum + = B [I]. If sum is greater than max, the sum value is assigned to max. If sum is smaller than 0, the sum = 0 is defined. Until the loop ends, get the sum of the largest sub-array.

2. Source code

1 // return the maximum sub-array and maximum value (with loops) of a one-dimensional integer array 2 package ketang; 3 import java. util. *; 4 public class SumArray {5 public static void main (String [] args) {6 bytes sca = new bytes (System. in); 7 System. out. println ("Number of input integer arrays"); 8 int num = sca. nextInt (); 9 int a [], B []; 10 a = new int [num]; 11 B = new int [num]; 12 System. out. println ("Number of input arrays"); 13 int I; 14 for (I = 0; I <num; I ++) 15 {16 a [I] = sca. nextInt (); 17} 18 int t, j = 0, sum = 0, max = a [0]; 19 for (I = 0; I <num; I ++) // before the ring is completed, find the maximum value 20 {21 sum + = a [I]; 22 if (max <sum) 23 {24 max = sum; 25} 26 if (sum <0) 27 {28 sum = 0; 29} 30} 31/* achieve first-end and end-to-end connection */32 int min = a [0]; 33 for (I = 0; I <num; I ++) // locate the minimum value 34 {35 if (min> a [I]) 36 {37 min = a [I]; 38 j = I; 39} 40} 41 t = j; // assign the minimum subscript j value to t42 I = 0; 43 while (j <num) 44 {45 B [I] = a [j]; 46 I ++; 47 j ++; 48} 49 j = 0; 50 while (j <t) 51 {52 B [I] = a [j]; 53 I ++; 54 j ++; 55} 56 sum = 0; // initialize sum as 057 for (I = 0; I <num; I ++) // After the link is connected, find the maximum value 58 {59 sum + = B [I]; 60 if (max <sum) 61 {62 max = sum; 63} 64 if (sum <0) 65 {66 sum = 0; 67} 68} 69 System. out. println ("Maximum subarray sum" + max); 70} 71}The Main Code

3. Results

4. Programming Summary

This programming only needs to be improved on the basis of the original program. The difficulty lies in how to find the key location of the link, locate the key location, form a new array, and then use the last idea to find the maximum value.

5. psp

Related Article

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.