题目链接:http://acm.ocrosoft.com/problem.php?cid=1172&pid=47
题目描述
任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和。
当n=7共14种拆分方法:
7=1+1+1+1+1+1+1 7=1+1+1+1+1+2 7=1+1+1+1+3 7=1+1+1+2+2 7=1+1+1+4 7=1+1+2+3 7=1+1+5 7=1+2+2+2 7=1+2+4 7=1+3+3 7=1+6 7=2+2+3 7=2+5 7=3+4 total=14
输入
输入n。
输出
按字典序输出具体的方案。
样例输入
7
样例输出
7=1+1+1+1+1+1+1
7=1+1+1+1+1+2
7=1+1+1+1+3
7=1+1+1+2+2
7=1+1+1+4
7=1+1+2+3
7=1+1+5
7=1+2+2+2
7=1+2+4
7=1+3+3
7=1+6
7=2+2+3
7=2+5
#include<bits/stdc++.h>
using namespace std;
int n,r;
int a[555];
int ans=0;
bool vis[22];
void dfs(int x,int cnt){
if(x==n){
cout<<n<<"=";
for(int i=1;i<cnt-1;i++){
cout<<a[i]<<"+";
}
cout<<a[cnt-1]<<endl;
return;
}else{
for(int i=a[cnt-1];i<n;i++){
if(x+i<=n){
a[cnt]=i;
dfs(x+i,cnt+1);
}
}
}
}
int main(){
cin>>n;
a[0]=1;
dfs(0,1);
return 0;
}