Выдает так:
4+
3+1+
2+2+
2+1+1+
1+1+1+1+
а должно быть вот так:
4
3+1
2+2
2+1+1
1+1+1+1
#include<iostream>
#include<vector>
using namespace std;
void printPartition(vector<int>& partition) {
for (int i = 0; i < partition.size(); i++) {
cout << partition << '+';
}
cout << endl;
}
void generatePartitions(int n, int maximum, vector<int>& currentPartition) {
if (n == 0) {
printPartition(currentPartition);
return;
}
for (int i = maximum; i >= 1; i--) {
if (i > n) {
continue;
}
currentPartition.push_back(i);
generatePartitions(n - i, i, currentPartition);
currentPartition.pop_back();
}
}
int main() {
int n;
cin >> n;
vector<int> currentPartition;
generatePartitions(n, n, currentPartition);
return 0;
}
4+
3+1+
2+2+
2+1+1+
1+1+1+1+
а должно быть вот так:
4
3+1
2+2
2+1+1
1+1+1+1
#include<iostream>
#include<vector>
using namespace std;
void printPartition(vector<int>& partition) {
for (int i = 0; i < partition.size(); i++) {
cout << partition << '+';
}
cout << endl;
}
void generatePartitions(int n, int maximum, vector<int>& currentPartition) {
if (n == 0) {
printPartition(currentPartition);
return;
}
for (int i = maximum; i >= 1; i--) {
if (i > n) {
continue;
}
currentPartition.push_back(i);
generatePartitions(n - i, i, currentPartition);
currentPartition.pop_back();
}
}
int main() {
int n;
cin >> n;
vector<int> currentPartition;
generatePartitions(n, n, currentPartition);
return 0;
}