Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int N, count = 0;
- cout << "Количество чисел" << endl;
- cin >> N;
- int arr[N];
- cout << "Введите числа" << endl;
- for (int i = 0; i < N; i++)
- cin >> arr[i];
- for (int i = 0; i < N -1 ; i++)
- if (arr[i] < 0 && arr[i+1] >= 0 || arr[i] >= 0 && arr[i+1] < 0)
- count++;
- cout << count << endl;
- template<class InputIterator>
- typename std::iterator_traits<InputIterator>::difference_type
- count_sign_reversals(InputIterator first, InputIterator last) {
- typename std::iterator_traits<InputIterator>::difference_type count = 0;
- if (first == last)
- return count;
- bool sign = *first++ < 0;
- for ( ; first != last; ++first)
- if (sign != (*first < 0)) {
- sign = !sign;
- ++count;
- }
- return count;
- }
- int main() {
- std::istream_iterator<int> numbers(std::cin), eof;
- std::cout << count_sign_reversals(numbers, eof) << std::endl;
- }
- #include <algorithm>
- #include <iostream>
- #include <iterator>
- int main()
- {
- std::istream_iterator<int> numbers(std::cin), eof;
- decltype(std::distance(numbers, eof)) count = 0;
- if (numbers != eof) {
- count = count_if(numbers, eof, [](int n) {
- static bool sign = n < 0;
- bool changed_sign = (sign != (n < 0));
- if (changed_sign) sign = !sign;
- return changed_sign;
- });
- }
- std::cout << count << std::endl;
- }
Add Comment
Please, Sign In to add comment