I got an assignment on the pros, I don’t understand them at all, but, unfortunately, I don’t have time to figure it out. Can anyone help with this task?
Find the sum of the array elements located after the maximum element.
I got an assignment on the pros, I don’t understand them at all, but, unfortunately, I don’t have time to figure it out. Can anyone help with this task?
Find the sum of the array elements located after the maximum element.
Well, let's say, this is the basis of the algorithm.
const int a[] = ...; // Исходный массив. int max = a[0]; // Максимальный элемент. int sum = 0; // Сумма. for (size_t i = 1; i < sizeof(a)/sizeof(a[0]); ++i) { if (a[i] > max) { max = a[i]; sum = 0; } else { sum += a[i]; } } #include <iostream> #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <conio.h> using namespace std; - tweekerusing should not swear. Maybe a semicolon is missing somewhere or a bracket or something. - user194374If you write in C ++, you should use the standard algorithms defined in the C ++ language.
In this case, the program may look like this.
#include <iostream> #include <algorithm> #include <numeric> #include <iterator> #include <cstdlib> #include <ctime> int main() { const size_t N = 20; int a[N]; std::srand((unsigned int)std::time(nullptr)); std::generate(std::begin(a), std::end(a), [=] { return std::rand() % N; }); for (int x : a) std::cout << x << ' '; std::cout << std::endl; int long long sum = std::accumulate( std::next(std::max_element(std::begin(a), std::end(a))), std::end(a), 0ll); std::cout << "sum = " << sum << std::endl; return 0; } The output of the program to the console may look like
8 17 16 11 9 10 5 1 3 17 6 17 3 6 4 12 10 11 1 3 sum = 145 In this program, the array is filled with random values from 0 to 20, and then it is written in one line how to find the sum of the elements after the maximum element.
If you are not familiar with standard C ++ algorithms, you can use regular cycles instead. For example,
#include <iostream> #include <cstdlib> #include <ctime> int main() { const size_t N = 20; int a[N]; std::srand((unsigned int)std::time(nullptr)); for ( int &x : a ) x = std::rand() % N; for (int x : a) std::cout << x << ' '; std::cout << std::endl; size_t max_i = 0; for ( size_t i = 1; i < N; i++ ) { if ( a[max_i] < a[i] ) max_i = i; } long long int sum = 0; for ( size_t i = max_i + 1; i < N; i++ ) sum += a[i]; std::cout << "sum = " << sum << std::endl; return 0; } In fact, this program is equivalent to the one shown above.
There is one subtlety to which attention should be paid. In your statement of the assignment it does not say what the maximum element should be the sum of the elements: after the first maximum element or after the last maximum element, since there can be several maximum elements in the sequence of numbers.
If you need to find the sum of elements after the last maximum element, then in this case, for example, in the last program with cycles, you should replace the condition in the sentence
if ( a[max_i] < a[i] ) max_i = i; ^^^ on
if ( a[max_i] <= a[i] ) max_i = i; ^^^^^ getch() function. But I swear that the "ID is not found." What is the problem? - tweekerFor example:
vector<int> z = {1,2,3,10,3,4,5}; cout << accumulate(max_element(z.begin(),z.end())+1,z.end(),0); Source: https://ru.stackoverflow.com/questions/616888/
All Articles