FB - BTKPTICH VinhDinhCoder

05/04/2023

an qua nho ke trong cay :3

Code
#include <bits/stdc++.h>
using namespace std;
int a[1000];

void solve(int sum,int idx, int n){
//	cout << sum << ' ' << idx << ' ' << n << endl;
    if (sum == n) {
        cout << n << " = ";
        for (int i=1;i< idx-1;i++) cout << a[i] << '+';
        cout << a[idx-1] << "\n";
    }
    else if (sum < n) {
        if (idx == 1) {
        	for (int i=1;i<= n/2;i++) {
	            if (sum + i <= n) {
	                a[idx] = i;
	                solve(sum+i, idx+1, n); 
	            }
	        }
		}
		else {
			for (int i=a[idx-1];i<= n;i++) {
				if (sum + i <= n) {
					a[idx] = i;
					solve(sum+i, idx+1, n);
				}
			}
		}
	}   
}

int main(){
    int n;
    cin >> n;
    solve(0,1, n);
    cout << n << " = " << n;
    return 0;
}