求助,不知道怎么解决排序和去除重复部分
查看原帖
求助,不知道怎么解决排序和去除重复部分
865793
better_Z楼主2023/6/14 20:37
#include<iostream>
using namespace std;
int n,a[10];
void dfs(int times,int now,int t){
    if(now>n)return;
    if(now==n){
        for(int i=1;i<=times-1;i++){
            if(i==1)cout<<a[i];
            else cout<<"+"<<a[i];
        }
        puts("");
        return;
    }
     for(int i=1;i<=n;i++){
        if(i==n)continue;
        a[t]=i;
        dfs(times+1,now+i,t+1);
        a[t]=0;
    }
}
int main(){
    cin>>n;
    dfs(1,0,1);
    return 0;
}
2023/6/14 20:37
加载中...