You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.2 KiB
69 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
int ans = 0, m;
|
|
vector<int> data;
|
|
|
|
void print();
|
|
|
|
void fun1(int n, int k) {//递减
|
|
if (n < 0) return;
|
|
if (n == 0) {
|
|
ans++;
|
|
print();
|
|
}
|
|
int i;
|
|
for (i = k; i >= 1; i--) {
|
|
data.push_back(i);
|
|
fun1(n - i, i);
|
|
|
|
for (vector<int>::iterator j = data.begin(); j != data.end(); j++) {
|
|
if (*j == i) {
|
|
data.erase(j);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void fun2(int n, int k) {//递增
|
|
if (n < 0) return;
|
|
if (n == 0) {
|
|
ans++;
|
|
print();
|
|
}
|
|
int i;
|
|
for (i = k; i <= n; i++) {
|
|
data.push_back(i);
|
|
fun2(n - i, i);
|
|
for (vector<int>::iterator j = data.begin(); j != data.end(); j++) {
|
|
if (*j == i) {
|
|
data.erase(j);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void print() {
|
|
int i;
|
|
cout << m << "=";
|
|
cout << data[0];
|
|
for (i = 1; i < data.size(); i++) {
|
|
cout << "+" << data[i];
|
|
}
|
|
cout << endl;
|
|
}
|
|
|
|
int main() {
|
|
m=5;
|
|
cout << "递减:" << endl;
|
|
fun1(m, m);
|
|
ans = 0;
|
|
cout << "递增:" << endl;
|
|
fun2(m, 1);
|
|
return 0;
|
|
}
|
|
|