Advertisement
Guest User

Untitled

a guest
May 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. const int n = 5;
  5. float a[n + 1];
  6. int fmax(float a[], int nach, int kon);
  7.  
  8. int main() {
  9. int i, k; for(i = 1; i <= n; i++) {
  10. cout << "n введи a[" << i << "] ";
  11. cin >> a[i];
  12. } for(i = 1; i <= n; i++) {
  13. cout << " a[" << i << "]=" << a[i];
  14. }
  15. cout << "n";
  16. cout << fmax(a[5], 0, 4); return(0);
  17. }
  18. int fmax(float a[], int nach, int kon)
  19. {
  20. int k, kmax;
  21. float max;
  22. kmax = nach;
  23. max = a[nach];
  24. for(k = nach; k <= kon; k++)
  25. {
  26. if(a[k] > max)
  27. {
  28. max = a[k]; kmax = k;
  29. }
  30. }
  31. return kmax;
  32. }
  33.  
  34. #include <iostream>
  35. #include <vector>
  36. #include <limits>
  37.  
  38. int max(const std::vector<int>::const_iterator& begin, std::vector<int>::const_iterator& end, int curentMax)
  39. {
  40. if(begin == end)
  41. return curentMax;
  42. if(*begin > curentMax)
  43. return max(begin + 1, end, *begin);
  44. return max(begin + 1, end, curentMax);
  45. }
  46.  
  47. int main()
  48. {
  49. std::vector<int> array{1, 55, 17, 77, 88, 13, 45, 72, 11};
  50. std::cout << max(array.сbegin(), array.сend(), std::numeric_limits<int>::min()) << "n";
  51. }
  52.  
  53. template<class ForwardIt>
  54. ForwardIt max_element(ForwardIt first, ForwardIt last, ForwardIt largest)
  55. {
  56. if (first == last) // no more elements to compare
  57. return largest;
  58.  
  59. if (*largest < *first) // compare with the first element
  60. largest = first;
  61.  
  62. ++first;
  63. return max_element(first, last, largest); // compare the rest
  64. }
  65.  
  66. template<class ForwardIt>
  67. ForwardIt max_element(ForwardIt first, ForwardIt last)
  68. {
  69. return max_element(first, last, first);
  70. }
  71.  
  72. #include <iostream>
  73.  
  74. int main()
  75. {
  76. float a[] = {1, -2, 3, 0.5};
  77. std::cout << *max_element(a, a + sizeof(a) / sizeof(*a)) << 'n';
  78. }
  79.  
  80. $ g++ max_recursive.cc && ./a.out
  81. 3
  82.  
  83. int* max_element(int* first, int* last, int* largest)
  84. {
  85. for ( ; first != last; ++first)
  86. if (*largest < *first)
  87. largest = first;
  88.  
  89. return largest;
  90. }
  91.  
  92. // Максимальный элемент в массиве array с индексами [start,stop)
  93. int maxel(int * array, int start, int stop)
  94. {
  95. if (start == stop-1) return array[start];
  96. int mid = (start + stop)/2;
  97. int m1 = maxel(array,start,mid), m2 = maxel(array,mid,stop);
  98. return (m1 > m2) ? m1 : m2;
  99. }
  100.  
  101.  
  102. int main(int argc, const char * argv[])
  103. {
  104. int x[] = { 1,2,15,2,41,18,-4,2 };
  105. cout << maxel(x,0,sizeof(x)/sizeof(x[0]));
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement