0
Amount n element in C++
Necessary to create the program removing amount n element in C++. example : n=2 1 2 1+2 n=3 1 2 3 1+2 1+3 2+3 1+2+3 n=4 1 2 3 4 1+2 1+3 1+4 2+3 2+4 3+4 1+2+3 1+2+4 2+3+4 1+2+3+4 and etc. thank you for attention . P.S. I bad understand english so has translated in program for translation .
5 Réponses
+ 6
Here we go, my friend. Had too much brain farting! 8D
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n,
i = 0,
group = 1;
do {
cout << "Enter the n (between 1 and 10): "; cin >> n;
} while (!(n >= 1 && n <= 10));
vector<int> v(n + 1);
for (auto &filler : v) filler = i++;
while (group < v.size()) {
size_t x = 0;
while (x < v.size()) { // **Not optimized**Need more thought**
size_t var_part = x + group;
while (var_part < v.size()) {
// fill the constant part of the series
for (size_t y = x + 1; y < (x + group); ++y)
cout << v[y] << " + ";
// fill the variable part of the series
if (var_part < v.size())
cout << v[var_part++] << endl;
else
break;
}
if (group == 1) x = v.size();
else ++x;
}
++group;
}
}
Sample output for n = 5
Enter the n (between 1 and 10):
1
2
3
4
5
1 + 2
1 + 3
1 + 4
1 + 5
2 + 3
2 + 4
2 + 5
3 + 4
3 + 5
4 + 5
1 + 2 + 3
1 + 2 + 4
1 + 2 + 5
2 + 3 + 4
2 + 3 + 5
3 + 4 + 5
1 + 2 + 3 + 4
1 + 2 + 3 + 5
2 + 3 + 4 + 5
1 + 2 + 3 + 4 + 5
______
https://code.sololearn.com/c0SzS97nRl05
+ 5
Don't worry I'll try and prepare an example for you to produce the same output as in the question.
+ 4
We do. But we get motivated when we see your attempt along with the problem description. Have you tried to implement it?
+ 3
I tried , but can't 😔 . algorithm very difficult .
+ 3
Good , very good . Thank you very much .
Отлично , очень хорошо . Огромное спасибо .