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.
python/TangDou/Dfs/DFS5_XiaoLanTuFaQiXiang.cpp

34 lines
569 B

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int n, m;
int cnt;
/**
测试数据:
5 1
答案:
3
*/
void dfs(int step, int sum1, int sum2) {
//收集答案
if (step == n + 1) {
if (sum1 - sum2 == m) cnt++;
return;
}
//选择了当前的数字
dfs(step + 1, sum1 + a[step], sum2);
//放弃了当前的数字
dfs(step + 1, sum1, sum2 + a[step]);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) a[i] = i;
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}