有哪位大佬能帮我把输出的顺序调换一下!!!
查看原帖
有哪位大佬能帮我把输出的顺序调换一下!!!
1350521
SimonLan楼主2024/8/29 22:26
#include<bits/stdc++.h>
using namespace std;
int a;
int ans[1000];
void dfs(int dep,int n){
	for(int i=n/2;i>=ans[dep-1];i--){
		int remain=n-i;
		ans[dep]=i;
		for(int j=1;j<=dep;j++){
			cout<<ans[j]<<"+";
		}
		cout<<remain<<"\n";
        dfs(dep+1,remain);
    }
}
int main(){
    cin>>a;
	ans[0]=1 ;
	dfs(1,a);
}

样例输入:7 样例输出:

1+1+1+1+1+1+1
1+1+1+1+1+2
1+1+1+1+3
1+1+1+2+2
1+1+1+4
1+1+2+3
1+1+5
1+2+2+2
1+2+4
1+3+3
1+6
2+2+3
2+5
3+4

我的输出:

3+4
2+5
2+2+3
1+6
1+3+3
1+2+4
1+2+2+2
1+1+5
1+1+2+3
1+1+1+4
1+1+1+2+2
1+1+1+1+3
1+1+1+1+1+2
1+1+1+1+1+1+1
2024/8/29 22:26
加载中...