'commit'
This commit is contained in:
68
TangDou/XiTi/1314_2.cpp
Normal file
68
TangDou/XiTi/1314_2.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user