C语言 递归拆分数字

来源:互联网 发布:淘宝造物节官网 编辑:程序博客网 时间:2024/05/27 21:12
#include<stdio.h>int stack[100];int top;int total,n;void dfs(int index){  int i;  if(total==n)  {  printf("%d=",n);  for(i=0;i<top-1;i++)  printf("%d+",stack[i]);  printf("%d\n",stack[top-1]);  }  if(total>n )  return ;  for(i=index;i>=1;i--)  {  total+=i;  stack[top++]=i;  dfs(i);  total-=i;  stack[--top];  }}void main(){while(scanf("%d",&n)!=EOF){top=0;total=0;dfs(n-1);}}

0 0