Solution to a problem - Digital decomposition

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Digital decomposition

Description
Enter a number N, it is divided into at least two and the sum of the numbers
and to ensure output scheme lexicographic, i.e. the first number as small as possible, in the case of the first one and the same second digit numbers as small as possible
, for example, n = 6
. 1. 5
2. 4
. 3. 3
. 1. 1. 4
. 1. 3 2
2 2 2
. 1. 1. 1. 3
. 1 2. 1 2
. 1. 1. 1. 1 2
. 1. 1. 1. 1. 1. 1

Input
a number N
the Output
plurality of rows, each row one approach, each digital space after each line has a

#include<bits/stdc++.h>
using namespace std;
int ans[100],n,lim;
void shuzidefenjie (int dep,int pre,int sum)
{
    if (dep==lim+1)
    {
        if (sum==n)
        {
            for (int i=1;i<=dep-1;i++)
                cout<<ans[i]<<" ";
            cout<<endl;
        }
        return;
    }
    for (int i=pre;i<=n;i++)
        if (sum+i<=n)
        {
            ans[dep]=i;
            shuzidefenjie(dep+1,i,sum+i);
        }
}
int main()
{
    cin>>n;
    for (lim=2;lim<=n;lim++)//这里是不可以在int一次的 (int lim;xx;xx)是错误的!!!!!!!!!! 
        shuzidefenjie(1,1,0);
    return 0;
}

Guess you like

Origin blog.csdn.net/Tangwan_jeff/article/details/95201045