Problem O: 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 )

Problem O: digital Decomposition
Time Limit:. 1 Sec Memory Limit: 128 MB
the Submit: 126 the Solved: 76
[the Submit] [the Status] [the Web Board]
the Description
enter a number N, it is divided into at least two digital sum
and to ensure the output scheme lexicographic order, i.e. the first number as small as possible, at one and the same number of digits as the case of the small second
example. 6 = n-
. 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
number of lines of a scheme, each number after each line of a space
HINT
This question is not difficult, you may have completed the split of the natural numbers, should be no problem.

#include<iostream>
#include<cstdio>
using namespace std;
int n, p[11]={1}, cnt=1, m;
void dfs(int dep,int cnt,int ans){
    if(dep==m+1){
        if(ans==n){
            for(int i=1;i<=dep-1; i++)
            cout<<p[i]<<" ";
            cout<<endl;
        }
        return;
    }
    for(int i=cnt;i<=n;i++){
        if(ans+i<=n){
            p[dep]=i;
            dfs(dep+1,i,ans+i);
        }
    }
}
int main(){
    cin>>n;
    for(m=2;m<=n;m++)
    dfs(1,1,0);
    return 0;
}

Guess you like

Origin blog.csdn.net/ebirth/article/details/95229510