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 |
package
shuzhi;
import
java.util.Scanner;
public
class
shuzuzhi {
public
static
void main(String[] args) {
// TODO Auto-generated method stub
Scanner
in
=
new
Scanner(System.
in
);
System.out.println(
"请输入数组长度:"
);
int n=
in
.nextInt();
int array[]=
new
int[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)