@@ -17,28 +17,46 @@ public static int max(int[] a){
17
17
}
18
18
System .out .println ("sum=" +sum );
19
19
20
- return maxDetail (a ,sum ,0 ,a .length -1 ,0 );
20
+ //return maxDetail(a,sum,0,a.length-1,0);
21
+
22
+ return maxDetail (a ,0 ,a .length -1 ,0 );
23
+
21
24
}
22
25
23
- public static int maxDetail (int [] a ,int sum ,int leftIndex ,int rightIndex ,int max ){
24
- int chooseNum = 0 ;
25
- if (rightIndex -leftIndex >1 ){
26
- int leftMax = sum - maxDetail (a ,sum -a [leftIndex ],leftIndex +1 ,rightIndex ,max );
27
- int rightMax = sum - maxDetail (a ,sum -a [rightIndex ],leftIndex ,rightIndex -1 ,max );
26
+ //public static int maxDetail(int[] a,int sum,int leftIndex,int rightIndex,int max){
27
+ // if(rightIndex-leftIndex>1){
28
+ // int leftMax = sum - maxDetail(a,sum-a[leftIndex],leftIndex+1,rightIndex,max);
29
+ // int rightMax = sum - maxDetail(a,sum-a[rightIndex],leftIndex,rightIndex-1,max);
30
+ //
31
+ // max = max + Math.max(leftMax,rightMax);
32
+ // System.out.println("leftMax="+leftMax+", rightMax="+rightMax+", Math.max(leftMax,rightMax)="+Math.max(leftMax,rightMax));
33
+ // System.out.println("max = " + max);
34
+ // return max;
35
+ // }else {
36
+ // System.out.println("leftMax="+a[leftIndex]+", rightMax="+a[rightIndex]+", Math.max(leftMax,rightMax)==== "+Math.max(a[leftIndex],a[rightIndex]));
37
+ // return Math.max(a[leftIndex],a[rightIndex]);
38
+ // }
39
+ //}
28
40
29
- chooseNum = Math .max (leftMax ,rightMax );
30
- return chooseNum ;
41
+ public static int maxDetail (int [] a ,int leftIndex ,int rightIndex ,int max ){
42
+ if (rightIndex -leftIndex >1 ){
43
+ int chooseNum = maxDetail (a ,leftIndex +1 ,rightIndex ,max )>maxDetail (a ,leftIndex ,rightIndex -1 ,max )?
44
+ a [rightIndex ]:a [leftIndex ];
45
+ max = max + chooseNum ;
46
+ return max ;
31
47
}else {
32
48
return Math .max (a [leftIndex ],a [rightIndex ]);
33
49
}
34
50
}
35
51
36
52
public static void main (String [] args ) {
37
- int v [] = { 1 , 2 , 3 , 6 , 9 , 5 , 7 , 4 , 2 , 6 , 9 , 5 , 8 , 7 , 2 , 1 , 55 , 3 , 6 , 9 , 7 , 5 , 2 };
53
+ //int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
54
+ int v [] = { 10 , 70 ,3 ,2 ,8 ,900 };
55
+
38
56
int n = max (v );
39
57
System .out .println (n );
40
58
41
- System .out .println (max2_dync (v ));
59
+ // System.out.println(max2_dync(v));
42
60
}
43
61
44
62
/**
0 commit comments