【问题标题】:How do I calculate the sum of all the array numbers after the first negative?如何计算第一个负数后所有数组数字的总和?
【发布时间】:2022-01-03 20:51:24
【问题描述】:

你能帮我解决这个问题吗?我所能做的就是计算所有的负数。 这是我的代码:

using namespace std;
int main()

{
    const int SIZE = 10;
    int arr[SIZE]{};
    int number=0;
    srand(time(NULL));
    cout << "Your array is: " << endl;
    for (int i=0; i<SIZE; i++)
    {
        int newValue = rand()%20-10;
        arr[i] = newValue;
        cout << arr[i] << " ";
        if (arr[i] < 0)
        {
            for (int j=-1; j<SIZE; j++)
            {
                number = arr[i];
                sum += fabs(number);
                break;
            }
        }
    }
    cout << endl;
    cout << "Sum of elements after first element < 0 is: " << sum;
    cout << endl;
}

【问题讨论】:

  • 只存储第一个负数的索引。 例如 int firstNegativeIndex = -1。如果遇到负数,则使用当前索引对其进行更新,前提是其当前值为 -1。然后,在读入整个数组后,从 firstNegativeIndex + 1 的值循环到 SIZE - 1,并对这些索引处的所有数组值求和。
  • 解决这个难题的诀窍是向后思考:从数组的末尾开始添加东西到开头,当你看到一个负数时就停止。这听起来是不是很简单?
  • @SamVarshavchik 但这将是最后一个否定

标签: c++ arrays sum


【解决方案1】:

一种方法是在第一个负数之后打开一个零标志:

int flag = 0;
int sum = 0;
for (std::size_t i = 0; i < SIZE; ++i){
    sum += flag * arr[i];
    flag |= arr[i] < 0;
}

这种方法的优点是您根本不需要数组:将标准输入中的下一个数字替换为 arr[i] 就足够了。

【讨论】:

    【解决方案2】:

    在您的特定案例中,有许多简单而有效的解决方案,例如that offered by Bathsheba

    但是,对于在满足给定条件的第一个值之后对数组中的元素求和的更一般情况,您可以使用 STL 中的 std::find_ifstd::accumulate 函数,提供适当的 lambda 函数来进行测试(检查是否为负数)和求和(代码中的sum += fabs(number) 表示您想要对剩余元素的绝对值求和1)。

    这是一个可能的实现:

    #include <cstdlib>    // std::abs, std::rand
    #include <ctime>      // std::time
    #include <algorithm>  // std::find_if
    #include <numeric>    // std::accumulate
    
    #include <iostream>
    using std::cout, std::endl;
    
    int main()
    {
        const int SIZE = 10;
        int arr[SIZE]{};
        // Generate random array...
        std::srand(static_cast<unsigned int>(time(nullptr)));
        cout << "Your array is: " << endl;
        for (int i = 0; i < SIZE; i++) {
            int newValue = std::rand() % 20 - 10;
            arr[i] = newValue;
            cout << arr[i] << " ";
        }
        // Sum all abs values after first negative ...
        auto is_neg = [](int i) { return i < 0; };
        auto fn = std::find_if(std::begin(arr), std::end(arr), is_neg);
        auto sum_abs = [](int a, int b) { return a + std::abs(b); };
        // Set the sum to ZERO if the first negative is the last element...
        int sum = (fn == std::end(arr)) ? 0 : std::accumulate(++fn, std::end(arr), 0, sum_abs);
        cout << endl;
        cout << "Sum of elements after first element < 0 is: " << sum;
        cout << endl;
        return 0;
    }
    

    1 如果不是 的情况,而您只想要 实际 值的总和,那么您可以省略 4th (sum_abs) 参数在对 std::accumulate 的调用中(以及该 lambda 的定义)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2011-05-27
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多