《奇怪的拆分》
查看原帖
《奇怪的拆分》
1037200
lty2023楼主2024/11/24 10:30

样例7,输出:

1+1+1+1+1+1+0
1+1+1+1+1+1
1+1+1+1+2
1+1+1+2+2
1+1+1+2
1+1+2+2
1+1+3
1+2+2+2
1+2+2
1+3+2
1+3
2+2+2
2+3
3+3
4

代码:

#include<bits/stdc++.h>
using namespace std;
int n,sum[1000],a[1000];
void f(int x){
	if(sum[x-1]==n){
		for(int i=1;i<x-1;i++){
			cout<<a[i]<<"+";
		}
		cout<<a[x]<<endl;
	}
	for(int i=a[x-1]>=1?a[x-1]:1;i<=n;i++){
		if(sum[x-1]+i<=n){
			sum[x]=sum[x-1]+i;
			a[x]=i;
			f(x+1);
		}
	}
}
int main(){
	cin>>n; 
	f(1);
}
2024/11/24 10:30
加载中...