写一个数的所有加法算式

#include <iostream.h>
#include <string.h>
int a[100];
void dfs(int rem,int prior,int i)//rem->remainder,i->the i-th number
{
    if(rem<0) return ;        
    if(rem==0)
    {while(i>=1) cout<<a[--i]<<'	';cout<<endl;return;}// disp the result
    for(int j=prior;j>=1;j--) //the following value is less than of equal the prior
        {a[i]=j;dfs(rem-j,j,i+1);}
}

void main()
{
    int n;
    memset(a,0,sizeof(a));
    cin>>n;
    dfs(n,n-1,0);
}
原文地址:https://www.cnblogs.com/ewitt/p/6623663.html