@[麒然](/space/show?uid=105266)
```cpp
#include<bits/stdc++.h>
using namespace std;
#define maxn 20
int c[maxn][maxn];//储存杨辉三角
int ans[maxn];
bool flag=false,vis[maxn];
int n,sum;
void yh(int N)
{//计算杨辉三角
// for(int i=0;i<N;i++){
// c[i][0]=1;
// c[i][i]=1;
// }
c[0][0] = 1;
for(int i=1;i<=N;i++){
for(int j=1;j<=i;j++){
c[i][j]=c[i-1][j-1]+c[i-1][j];
//cout << c[i][j] << ' ' ;
}
//cout << endl;
}
}
void dfs(int x,int y)
{
if(flag==true)return ;
else if(x>n)
{
if(y==sum)
{//符合条件输出
for(int i=1;i<=n;i++)
{
cout << ans[i] << " ";
}
flag=true;
return ;
}
else return ;
}
else if(y>sum)
{
return ;
}
else
for(int i=1;i<=n;i++)
if(!vis[i])
{
ans[x]=i;
vis[i]=true;//标记已用过
dfs(x+1,y+i*c[n][x]);
vis[i]=false;//回溯
}
}
int main(){
cin>>n>>sum;//输入
yh(n);//计算杨辉三角
dfs(1,0);
return 0;
```
学长,您杨辉三角好像写错了
by paulyc @ 2019-10-31 19:42:05