7-6 整数分解为若干项之和 (15 分)

 

 

#include <iostream>
using namespace std;
const int N = 40;
int q[N];
int k, n;

void dfs(int st, int cnt, int total)
{
    if(total == n)
    {
        k++;
        cout << n << "=";
        cout << q[0];
        for(int i = 1; i < cnt; i++) cout << "+" << q[i];
        if((k % 4 == 0)) cout << endl;
        else if(st != n) cout << ";";
        return;
    }
    if(total > n) return;
    
    if(total < n)
    {
        for(int i = st; i <= n; i++)
        {
            q[cnt] = i;
            dfs(i, cnt+1, total + i);
        }
    }
}
int main()
{
    cin >> n;
    dfs(1, 0, 0);
    
    return 0;
}

 

posted @ 2022-03-03 20:07  飘向远方丶  阅读(58)  评论(0)    收藏  举报