和此题类似的站外题求助
查看原帖
和此题类似的站外题求助
677581
_Glassy_Sky_楼主2023/7/19 20:04

题目

#include<bits/stdc++.h>
using namespace std;
int n;
int a[10] = {1};
void print(int t)
{
	cout << n << "=";
	for(int i = 1; i < t; i ++)
		cout << a[i] << "+";
	cout << a[t] << "\n";
} 
int dfs(int s, int t)
{
	for(int i = a[t - 1]; i <= s; i ++)
	{
		if(i < n)
		{
			a[t] = i;//标记当前位置
			s -= i;//减去此数
			if(s == 0)//分配完毕 
				print(t); 
			else
				dfs(s, t + 1);//继续拆分 
			s += i;//回溯,递推 
		}
	}
}
int main()
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	dfs(n, 1);
	return 0;
}
2023/7/19 20:04
加载中...