1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-27 08:26:20 +00:00
202401-programming-assignments/【实践课外】11.函数2/6-4 分治法求解金块问题.c

26 lines
483 B
C

int max(int a[], int m, int n) {
if (m == n) {
return a[m];
}
int mid = (m + n) / 2;
int left_max = max(a, m, mid);
int right_max = max(a, mid + 1, n);
return left_max > right_max ? left_max : right_max;
}
int min(int a[], int m, int n) {
if (m == n) {
return a[m];
}
int mid = (m + n) / 2;
int left_min = min(a, m, mid);
int right_min = min(a, mid + 1, n);
return left_min < right_min ? left_min : right_min;
}