如何计算第一个负数后所有数组数字的总和?
How do I calculate the sum of all the array numbers after the first negative?
你能帮我解决这个问题吗?我所能做的就是数一数所有的负数。
这是我的代码:
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 flag = 0;
int sum = 0;
for (std::size_t i = 0; i < SIZE; ++i){
sum += flag * arr[i];
flag |= arr[i] < 0;
}
这种方法的优点是您根本不需要数组:用标准输入中的下一个数字替换 arr[i]
就足够了。
在您的具体案例中,有许多简单有效的解决方案,例如。
但是,对于在第一个值满足给定条件后对数组中的元素求和的更一般情况,您可以使用 STL 中的 std::find_if
and std::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 如果不是这种情况,您只需要实际[=34]的总和=] 值,那么您可以在对 std::accumulate
的调用(以及该 lambda 的定义)中省略第 4th (sum_abs
) 参数。
你能帮我解决这个问题吗?我所能做的就是数一数所有的负数。 这是我的代码:
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 flag = 0;
int sum = 0;
for (std::size_t i = 0; i < SIZE; ++i){
sum += flag * arr[i];
flag |= arr[i] < 0;
}
这种方法的优点是您根本不需要数组:用标准输入中的下一个数字替换 arr[i]
就足够了。
在您的具体案例中,有许多简单有效的解决方案,例如
但是,对于在第一个值满足给定条件后对数组中的元素求和的更一般情况,您可以使用 STL 中的 std::find_if
and std::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 如果不是这种情况,您只需要实际[=34]的总和=] 值,那么您可以在对 std::accumulate
的调用(以及该 lambda 的定义)中省略第 4th (sum_abs
) 参数。