http://ybt.ssoier.cn:8088/problem_show.php?pid=1318
#include<bits/stdc++.h>
#define UP(i,x,y) for(int i=x; i<=y; i++)
using namespace std;
int N = 7;
int ans = 0;
vector<int> arr;
/*
枚举思想。放好第一位放第二位....
frontVal:上一个位置放了什么数字
nowSum: 上一个数字放完后,数字总和
*/
void dfs(int frontVal, int nowSum)
{
// 出口
if(nowSum == N)
{
UP(i, 0, arr.size()-1)
{
cout<<arr[i]<<" ";
}
cout<<endl;
ans ++;
return ;
}
/*
由于是字典序,所以每次放的数字大于等于frontVal
又由于放的数字不能为N所以 N-1
*/
UP(i, frontVal, N-1)
{
if(i + nowSum <= N)
{
arr.push_back(i); // 放的数字记录在数组中
dfs(i, nowSum+i);
arr.pop_back(); // 放的数字记录从数组中移除
}
}
}
int main()
{
dfs(1, 0);
cout<<ans;
return 0;
}