There is a sequence of real numbers [latex]a_1, a_2, \ldots[/latex](read to the end of the input stream). You need to get the sequence of numbers [latex]b_1, \ldots, b_{10}[/latex], where [latex]b_i[/latex] is the sum of those members of input sequence, that belong left-open interval [latex](i — 1, i](i = 1, \ldots, 10)[/latex]. If the interval doesn’t contain any members of the sequence, the corresponding [latex]b_i[/latex] will be set equal to zero.
Input
The sequence of real numbers [latex]a_1, a_2, \ldots[/latex].
Output
Output the sequence [latex]b_1, \ldots, b_{10}[/latex], that satisfies specified conditions.
Tests
№ | Input sequence | Output sequence |
1 | 1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 |
2 | 1 1 1 1 1 1 1 | 7 0 0 0 0 0 0 0 0 0 |
3 | 2.1 2.5 2.7 | 0 0 7.3 0 0 0 0 0 0 0 |
4 | 11 12 13 14 15 | 0 0 0 0 0 0 0 0 0 0 |
5 | 2 12 85 0.6 | 0.6 2 0 0 0 0 0 0 0 0 |
6 | 2.02 42 1.998 3 7.43 3.33 3.03 5.56 5 5.5 | 0 1.998 5.02 6.36 5 11.06 0 7.43 0 0 |
Algorithm
The proposed task we can solve in two different ways: using class vector and like a problem with stream processing of data.
- Class vector:
Initialize a vector, that will store all the elements of the input sequence (push them to the vector to the end of the input stream). Further, sort it ascending for easy follow-up work. For the each element of initial sequence check whether it belongs to the current interval. If it is true, it will be added to the corresponding element of resulting sequence, or zero will be added otherwise. Output the resulting sequence. - Stream processing:
While we are reading the input stream, we can determine which element of the resulting sequence it belongs, by using rounding to smallest integral value that is not less that our and substracting one (we need to remember that we can’t go beyond the bounds of the array, therefore we use a separate check). Perform the output of the result.
Code (class Vector)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <vector> #include <iostream> #include <algorithm> using namespace std; const unsigned int RESULTING_SEQUENCE_SIZE = 10; int main() { double currentMember; vector <double> inputSequence; double resultingSequence[RESULTING_SEQUENCE_SIZE]{0}; // Adds a new element to the vector to the end of the input stream while (cin >> currentMember) { inputSequence.push_back(currentMember); } sort(inputSequence.begin(), inputSequence.end()); // Sort ascending initial sequence for (int i = 0; i < RESULTING_SEQUENCE_SIZE; i++) { // Accumulate the sum of suitable elements to the corresponding member of the result sequence for (auto currentMember : inputSequence) { resultingSequence[i] += (i < currentMember && currentMember <= i + 1 ? currentMember : 0); } } // Output the sequence for (auto currentMember : resultingSequence) { cout << currentMember << " "; } return 0; } |
Code (stream processing)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <cmath> #include <iostream> using namespace std; const unsigned int RESULTING_SEQUENCE_SIZE = 10; int main() { double currentMember; double resultingSequence[RESULTING_SEQUENCE_SIZE]{0}; // Process until the end of the input stream while (cin >> currentMember) { // Accumulate the sum of suitable elements to the corresponding member of the resulting sequence if (1 <= ceil(currentMember) && ceil(currentMember) <= RESULTING_SEQUENCE_SIZE) { resultingSequence[(int)ceil(currentMember) - 1] += currentMember; } } // Output the sequence for (auto currentMember : resultingSequence) { cout << currentMember << " "; } return 0; } |