The largest element in the collection of divide and conquer actions, and the collection of divide and conquer actions
Question: Enter n and then enter n integers. Use the divide and conquer method to calculate the largest element in the n number;
Train of Thought: divide the number of columns into two halves, recursively go down, stop when there is only one number left, return this number, if not one number, return the larger number of the two segments to be divided;
Experiment tip: Find the largest element in the n Data Element Set. When n = 2, you can find the maximum and minimum elements of the two data elements after a comparison. When n> 2, n data elements can be divided into roughly equal two halves, with n/2 data elements in half and n/2 data elements in the other half. Find the largest element in each group, and compare the two largest elements to obtain the largest element of n.
The Code is as follows:
# Include <iostream> # include <algorithm> # include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h> # include <set> # include <queue> # include <vector> using namespace std; int calc (int a [], int l, int r) {if (l = r) return a [l]; return max (calc (a, l, (l + r)/2), calc (, (l + r)/2 + 1, r);} int main () {int a [1005]; int n; cout <"sequence length: "<endl; scanf (" % d ", & n); if (n = 0) {puts (" the sequence length cannot be 0! "); Return 0 ;}cout <" enter a sequence: "<endl; for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); printf ("the largest element of this series is: % d \ n", calc (a, 0, n-1 )); return 0 ;}