Вычислить сумму модулей элементов массива,
расположенных после первого отрицательного элемента.
C/C++
Программирование на c++
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
using namespace std;
int main() {
int box[] = { 47, 76, 68, -5, 45, -5, 5 };
for (auto x : box) cout << setw(5) << x;
puts("");
auto negate = [](int x) { return x < 0; };
auto pos = find_if(begin(box), end(box), negate) + 1;
if (pos < end(box)) {
auto summod = [](int s, int x) { return s += abs(x); };
auto sum = accumulate(pos, end(box), 0, summod);
cout << "Sum: " << sum << '\n';
} else {
puts("Not found!");
}
system("pause > nul");
}
#include <iostream>
#include <iomanip>
#include <numeric>
using namespace std;
int main() {
int box[] = { 47, 76, 68, -5, 45, -5, 5 };
for (auto x : box) cout << setw(5) << x;
puts("");
auto negate = [](int x) { return x < 0; };
auto pos = find_if(begin(box), end(box), negate) + 1;
if (pos < end(box)) {
auto summod = [](int s, int x) { return s += abs(x); };
auto sum = accumulate(pos, end(box), 0, summod);
cout << "Sum: " << sum << '\n';
} else {
puts("Not found!");
}
system("pause > nul");
}
#include "iostream"
#include "ctime"
#include "cstdlib"
#include "cmath"
#include "algorithm"
using namespace std;
int main(){
int n,*a; cout<<"n: "; cin>>n; a=new int[n]; srand(time(NULL));
for(int i=0;i< n;i++)cout<<(a[i]=rand()%100-50)<<' ';
int *k=find_if(a,a+n,[](int x){return x<0;});
int s=accumulate(k+1,a+n,0,[](int x,int y){return x+abs(y);});
cout<<"\nsum="<< s<<'\n';}
#include "ctime"
#include "cstdlib"
#include "cmath"
#include "algorithm"
using namespace std;
int main(){
int n,*a; cout<<"n: "; cin>>n; a=new int[n]; srand(time(NULL));
for(int i=0;i< n;i++)cout<<(a[i]=rand()%100-50)<<' ';
int *k=find_if(a,a+n,[](int x){return x<0;});
int s=accumulate(k+1,a+n,0,[](int x,int y){return x+abs(y);});
cout<<"\nsum="<< s<<'\n';}
#include "iostream"
#include "cmath"
using namespace std;
int main(int argc, char *argv[])
{
int arr[] = {47, 76, 68, -5, 45, -5, 5};
bool start = false;
int sum = 0;
for (auto i : arr)
{
if (i < 0 and! start)
{
start = true;
continue;
}
if (start)
sum += abs(i);
}
cout << sum;
}
Тот самый случай, который я ждал всю жизнь, когда нужен continue
#include "cmath"
using namespace std;
int main(int argc, char *argv[])
{
int arr[] = {47, 76, 68, -5, 45, -5, 5};
bool start = false;
int sum = 0;
for (auto i : arr)
{
if (i < 0 and! start)
{
start = true;
continue;
}
if (start)
sum += abs(i);
}
cout << sum;
}
Тот самый случай, который я ждал всю жизнь, когда нужен continue
и что сложного... Находишь 1 отрицательный элемент в 1 цикле, во 2 цикле просто собираешь сумму этих модулей и всё
Похожие вопросы
- Программирование на C++
- Программирование на C++
- Помоги написать лабу по программированию на c++
- Помогите решить задачу по программированию на C++
- Программирование на C. Помогите бездарю
- На каком языке программирования написан C (Си)?
- Программирование на c++
- Программирование на C++
- Помогите с программированием на C++
- Задание по программированию язык C/СИ