Guest User

Untitled

a guest
Dec 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. int N, count = 0;
  2. cout << "Количество чисел" << endl;
  3. cin >> N;
  4.  
  5. int arr[N];
  6. cout << "Введите числа" << endl;
  7. for (int i = 0; i < N; i++)
  8. cin >> arr[i];
  9. for (int i = 0; i < N -1 ; i++)
  10. if (arr[i] < 0 && arr[i+1] >= 0 || arr[i] >= 0 && arr[i+1] < 0)
  11.  
  12. count++;
  13. cout << count << endl;
  14.  
  15. template<class InputIterator>
  16. typename std::iterator_traits<InputIterator>::difference_type
  17. count_sign_reversals(InputIterator first, InputIterator last) {
  18. typename std::iterator_traits<InputIterator>::difference_type count = 0;
  19. if (first == last)
  20. return count;
  21. bool sign = *first++ < 0;
  22. for ( ; first != last; ++first)
  23. if (sign != (*first < 0)) {
  24. sign = !sign;
  25. ++count;
  26. }
  27. return count;
  28. }
  29.  
  30. int main() {
  31. std::istream_iterator<int> numbers(std::cin), eof;
  32. std::cout << count_sign_reversals(numbers, eof) << std::endl;
  33. }
  34.  
  35. #include <algorithm>
  36. #include <iostream>
  37. #include <iterator>
  38.  
  39. int main()
  40. {
  41. std::istream_iterator<int> numbers(std::cin), eof;
  42. decltype(std::distance(numbers, eof)) count = 0;
  43. if (numbers != eof) {
  44. count = count_if(numbers, eof, [](int n) {
  45. static bool sign = n < 0;
  46. bool changed_sign = (sign != (n < 0));
  47. if (changed_sign) sign = !sign;
  48. return changed_sign;
  49. });
  50. }
  51. std::cout << count << std::endl;
  52. }
Add Comment
Please, Sign In to add comment