• Дан массив действительных чисел размерностью N. Подсчитать, сколько в нем отрицательных, положительных и нулевых элементов
    c++

Ответы 1

    1. #include <iostream>
    2. #include <array>
    3. #include <algorithm>
    4. #include <functional>
    5. template <std::size_t size>
    6. int count(std::array<int, size> const& arr_, std::function<bool(int)> const& f)
    7. {
    8.    return std::count_if(arr_.begin(), arr_.end(), f);
    9. }
    10. int main()
    11. {
    12.    std::array<int, 5> arr = { -2, 0, 5, 0, -1 };
    13.    int positive = count(arr, [](int a){ return a > 0; });
    14.    int negative = count(arr, [](int a){ return a < 0; });
    15.    int zero = count(arr, [](int a){ return a == 0; });
    16.    std::cout << positive << " " << negative << " " << zero;
    17. }
  • Добавить свой ответ

Войти через Google

или

Забыли пароль?

У меня нет аккаунта, я хочу Зарегистрироваться

How much to ban the user?
1 hour 1 day 100 years