2519: 【普及-】【P2404】自然数的拆分问题

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:8 解决:6

题目描述

任何一个大于 lns="http://www.w3.org/1998/Math/MathML">1 的自然数 lns="http://www.w3.org/1998/Math/MathML">,总可以拆分成若干个小于 lns="http://www.w3.org/1998/Math/MathML"> 的自然数之和。现在给你一个自然数 lns="http://www.w3.org/1998/Math/MathML">,要求你求出 lns="http://www.w3.org/1998/Math/MathML"> 的拆分成一些数字的和。每个拆分后的序列中的数字从小到大排序。然后你需要输出这些序列,其中字典序小的序列需要优先输出。

输入

输入:待拆分的自然数 lns="http://www.w3.org/1998/Math/MathML">

输出

输出:若干数的加法式子。

样例输入 复制

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

提示

说明/提示

数据保证,lns="http://www.w3.org/1998/Math/MathML">28

#include<bits/stdc++.h>
using namespace std;
int a[10];
void dfs(int s,int step) {
	if (s==0) {
		for (int i=0;i<step-1;i++) cout<<a[i]<<"+";
		cout<<a[step-1]<<endl;
	}
	for(int i=1;i<=s;i++) {
		if (i<a[step-1]) continue;
		a[step]=i;
		dfs(s-i,step+1);
	}
}
int main(){
    int n;
    cin>>n;
    for (int i=1;i<=n/2;i++) {
    	a[0]=i;
    	dfs(n-i,1);
	}
	return 0;
}