Classroom Exercises--7th week (two pairs)

Source: Internet
Author: User

Cooperation Process:

The last design ideas and programming language differences, through the communication discussion unity decided to use Java programming, Chen Peng responsible for programming, Li Guo responsible for code review and code testing.

Design ideas:

Set three variables, Max is (the maximum value in the Subarray and the initial value is Array[j]), RESULT1 (the current sub-array and the initial value is array[j]);

Let the user set the length of the array first, and then input the array (the input array is judged by whether it is all negative);

Add a For loop to the end-to-end phase before traversing;

After the first bit from the array to begin traversing the array, max=max+array[i], if result<0, then the max=0; if Max>result, then Result=max;

Finally, when the loop is finished, the value of the last result is output.

Problems encountered:

When the initial value is arrary[0], the first digit value cannot be negative when it is entered;

The maximum number of outputs is a number of negative numbers when all are negative;

Source:

Package Shuzhi;import Java.util.scanner;public class Shuzuzhi {public static void main (string[] args) {//TODO Auto-genera        Ted Method Stubscanner in=new Scanner (system.in);        System.out.println ("Please enter array length:");        int N=in.nextint ();        int array[]=new Int[n];        int result = 0;        Boolean bool=false;        System.out.println ("Please enter the corresponding integer:");        for (int i=0;i<n;i++) {array[i]=in.nextint ();        } for (int i=0;i<n;i++) {if (array[i]>0) bool=true; }//Determine if all are negative if (bool)//Not all negative {//Circle of loop for (int j=0;j<n;j++) {int max=         ARRAY[J];           int RESULT1 =array[j];              for (int i=0;i<n-j;i++) {max=max+array[i];              if (MAX&GT;RESULT1) {Result1=max;              } if (max<0) {max=0;          }} RESULT=RESULT1;    }        }    else//are all negative {int max=array[0];        for (int i=1;i<n;i++) {if (Max<array[i]) max=array[i];        } Result=max; } System.out.print ("The maximum value of the Subarray is:" +result);}}

Summarize:

Enter the initial value to understand whether the initial value needs to change, using the For loop to determine whether the scope is correct;

Classroom Exercises--7th week (two pairs)

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.