Classroom exercises (array pair development)

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:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 packageshuzhi;importjava.util.Scanner;public classshuzuzhi {    public staticvoid main(String[] args) {        // TODO Auto-generated method stub        Scanner in=newScanner(System.in);        System.out.println("请输入数组长度:");        int n=in.nextInt();        int array[]=newint[n];        int result = 0;        boolean bool=false;        System.out.println("请输入相应的整数:");        for(int i=0;i<n;i++)        {            array[i]=in.nextInt();        }        for(int i=0;i<n;i++)        {            if(array[i]>0)bool=true;        }     //判断是否全为负数             if(bool)//不全为负数      {             //圈的循环        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>result1)              {                result1=max;              }              if(max<0)              {               max=0;              }            }           result=result1;          }        }        else//全为负数        {            int max=array[0];            for(int i=1;i<n;i++)            {                if(max<array[i])                    max=array[i];            }            result=max;        }        System.out.print("子数组的最大值为:"+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 (array pair development)

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.