forked from embedded2015/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive.c
64 lines (55 loc) · 1.2 KB
/
recursive.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_ARR 100
int maxSubArray(int arr[], int l, int h);
int max_2(int a, int b)
{
return a > b? a : b;
}
int max_3(int a, int b, int c)
{
return max_2(a, b) > c? max_2(a, b) : c;
}
int maxCrossingSum(int arr[], int l, int m, int h);
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
int i = 0;
int A[MAX_ARR];
while(fscanf(fp, "%d", &A[i]) != EOF)
i++;
printf("%d\n", i);
fclose(fp);
int max_sum;
max_sum = maxSubArray(A, 0, i-1);
printf("%d\n", max_sum);
return 0;
}
int maxCrossingSum(int arr[], int l, int m, int h)
{
int sum = 0;
int left_sum = INT_MIN;
int i;
for(i = m; i >= l; i--) {
sum += arr[i];
if(sum > left_sum)
left_sum = sum;
}
sum = 0;
int right_sum = INT_MIN;
for(i = m + 1; i <= h; i++) {
sum += arr[i];
if(sum > right_sum)
right_sum = sum;
}
return right_sum + left_sum;
}
int maxSubArray(int arr[], int l, int h)
{
if(l == h)
return arr[l];
int m = (l + h)/2;
return max_3(maxSubArray(arr, l, m), maxSubArray(arr, m+1, h), maxCrossingSum(arr, l, m, h));
}