-
Notifications
You must be signed in to change notification settings - Fork 0
/
最大子段和
41 lines (39 loc) · 747 Bytes
/
最大子段和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class ziduanhe {
public static void main(String[] args) {
int[] a=new int[100];
int i=0;
int count=0;
Scanner sc = new Scanner(System.in);
count=sc.nextInt();
for(i=0; i<count; i++)
{
a[i]=sc.nextInt();
}
System.out.println(max(count,a));
}
public static int max(int count,int [] a) {
int[] b=new int[100];
int max,i;
b[0]=a[0];
max=b[0];
for(i=1; i<count; i++)
{
if(b[i-1]>0)
{
b[i]=b[i-1]+a[i];
}
else
{
b[i]=a[i];
}
if(b[i]>max)
{
max=b[i];
}
}
if(max<0)
max=0;
return max;
}
}