fork(1) download
  1. #include <iostream>
  2. #include <locale>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <numeric>
  8. #include <iterator>
  9. #include <limits>
  10. struct R { int operator()() const { return rand() % 21 - 10; } }; template <typename Iterator>
  11. Iterator next(Iterator it) { std::advance(it, 1); return it; } template <typename Iterator>
  12. void print(Iterator begin, Iterator end) { typedef std::ostream_iterator<typename std::iterator_traits<Iterator>::value_type> O;
  13. std::copy(begin, end, O(std::cout, " ")); std::cout << std::endl; } template <typename Iterator, typename F>
  14. void find(Iterator begin, Iterator end, F f) { typedef typename std::iterator_traits<Iterator>::value_type T;
  15. struct { T sum; Iterator begin, end; } max = { std::numeric_limits<T>::min(), begin, end }; std::cout <<
  16. "All Sequence: " << std::endl; print(max.begin, max.end); for (; (begin = std::find_if(begin, end, f)) != end;)
  17. { Iterator seq_end = std::find_if(next(begin), end, std::not1(f)); T sum = std::accumulate(begin, seq_end, T());
  18. if (sum > max.sum) { max.sum = sum; max.begin = begin; max.end = seq_end; } begin = seq_end; } if
  19. (max.sum != std::numeric_limits<T>::min()) { std::cout << "Sequence with a maximum sum: " << std::endl;
  20. print(max.begin, max.end); } else std::cout << "Sequence doesn't have positive numbers" << std::endl; }
  21. int main() { std::locale::global(std::locale("")); srand((unsigned) time(NULL)); int arr[20]; std::generate(arr,
  22. arr + sizeof(arr) / sizeof(arr[0]), R()); ::find(arr, arr + sizeof(arr) / sizeof(arr[0]), std::bind2nd(std::greater<int>(), 0));
  23. return 0; }
Success #stdin #stdout 0.01s 4896KB
stdin
Standard input is empty
stdout
All Sequence: 
-10 -9 3 5 0 5 8 6 -4 -4 -10 -2 1 -1 2 -8 3 -6 3 2 
Sequence with a maximum sum: 
5 8 6