Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int n = 5;
- float a[n + 1];
- int fmax(float a[], int nach, int kon);
- int main() {
- int i, k; for(i = 1; i <= n; i++) {
- cout << "n введи a[" << i << "] ";
- cin >> a[i];
- } for(i = 1; i <= n; i++) {
- cout << " a[" << i << "]=" << a[i];
- }
- cout << "n";
- cout << fmax(a[5], 0, 4); return(0);
- }
- int fmax(float a[], int nach, int kon)
- {
- int k, kmax;
- float max;
- kmax = nach;
- max = a[nach];
- for(k = nach; k <= kon; k++)
- {
- if(a[k] > max)
- {
- max = a[k]; kmax = k;
- }
- }
- return kmax;
- }
- #include <iostream>
- #include <vector>
- #include <limits>
- int max(const std::vector<int>::const_iterator& begin, std::vector<int>::const_iterator& end, int curentMax)
- {
- if(begin == end)
- return curentMax;
- if(*begin > curentMax)
- return max(begin + 1, end, *begin);
- return max(begin + 1, end, curentMax);
- }
- int main()
- {
- std::vector<int> array{1, 55, 17, 77, 88, 13, 45, 72, 11};
- std::cout << max(array.сbegin(), array.сend(), std::numeric_limits<int>::min()) << "n";
- }
- template<class ForwardIt>
- ForwardIt max_element(ForwardIt first, ForwardIt last, ForwardIt largest)
- {
- if (first == last) // no more elements to compare
- return largest;
- if (*largest < *first) // compare with the first element
- largest = first;
- ++first;
- return max_element(first, last, largest); // compare the rest
- }
- template<class ForwardIt>
- ForwardIt max_element(ForwardIt first, ForwardIt last)
- {
- return max_element(first, last, first);
- }
- #include <iostream>
- int main()
- {
- float a[] = {1, -2, 3, 0.5};
- std::cout << *max_element(a, a + sizeof(a) / sizeof(*a)) << 'n';
- }
- $ g++ max_recursive.cc && ./a.out
- 3
- int* max_element(int* first, int* last, int* largest)
- {
- for ( ; first != last; ++first)
- if (*largest < *first)
- largest = first;
- return largest;
- }
- // Максимальный элемент в массиве array с индексами [start,stop)
- int maxel(int * array, int start, int stop)
- {
- if (start == stop-1) return array[start];
- int mid = (start + stop)/2;
- int m1 = maxel(array,start,mid), m2 = maxel(array,mid,stop);
- return (m1 > m2) ? m1 : m2;
- }
- int main(int argc, const char * argv[])
- {
- int x[] = { 1,2,15,2,41,18,-4,2 };
- cout << maxel(x,0,sizeof(x)/sizeof(x[0]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement