user-0 提问: 用c语言将一个数分解成加数相加的形式

回复:
user: user-1
content: 仅供参考 #include <stdio.h> #include <stdlib.h>  #define MIN(X,Y) (((X)<(Y))?(X):(Y))  void WriteBuffer(const int * buffer) { 	while (buffer[1] != 0) 	{ 		printf("%d+", *buffer++); 	} 	printf("%d\n", *buffer); }  void _splitnum(int * buffer, const int x, const size_t pos) { 	if (x <= 1) 	{ 		buffer[pos + 1] = x; 		buffer[pos + 2] = 0; 		WriteBuffer(buffer); 	} 	else 	{ 		size_t npos = pos + 1; 		for (int i = 1; i <= MIN(x, buffer[pos]); i++) 		{ 			buffer[npos] = i; 			_splitnum(buffer, x - i, npos); 		} 	} }  void SplitNum(const int x) { 	if (x <= 0) return; 	int * buffer = (int*)malloc(sizeof(int) * (x + 1)); 	for (int i = 1; i < x; i++) 	{ 		buffer[0] = i; 		_splitnum(buffer, x - i, 0); 	} }  int main() { 	int x = 0; 	if (scanf("%d", &x)) 		SplitNum(x);     return EXIT_SUCCESS; }

user: user-2
content: “给定一个小点的输入，完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门！ 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出

user: user-2
content: #include <stdio.h> #include <stdlib.h> void print(int res[], int num) {     static int L=0;     L++;     printf("%8d:",L);     for (int i=0;i<num;++i) {         printf(" %d", res[i]);     }     printf("\n"); } void split(int n, int m) {// n表示总数，m表示最大因子     static int res[100];// 保存结果     static int num=-1;// 当前因子下标      if (n<m || n<0 || m<1) return;     num++;     if (0==n) {// 递归终止条件，为0不可再分，直接输出         print(res,num+1);         num--;         return;     } else {         if (n==m) {// 不拆，直接输出             res[num]=m;             print(res,num+1);             num--;         } else {             // 拆分出第一个             res[num]=m;             n=n-m;              if (m>n) m = n; // 最大因子不可能大于总数              for (int i=m;i>=1;--i) {// 循环，第二个因子可以继续拆分，而且按照最大因子不同可以拆分成多个                 split(n,i);             }             num--;         }     } } void Split(int n) {     if (n<=0) return;     if (100<n) {         printf("Up to 100\n");         return;     }     for (int i=n;i>=1;--i) {         split(n, i);     } } void main(int argc,char **argv) {          if (argc<=1) Split(5);     else if (argc>=3) split(atoi(argv[1]),atoi(argv[2]));     else              Split(atoi(argv[1])); }

user: user-3
content: 有点难度引用 2 楼 user-4 的回复:[quote=引用 1 楼 iyomumx 的回复:] 仅供参考 #include <stdio.h> #include <stdlib.h>  #define MIN(X,Y) (((X)<(Y))?(X):(Y))  void WriteBuffer(const int * buffer) { 	while (buffer[1] != 0) 	{ 		printf("%d+", *buffer++); 	} 	printf("%d\n", *buffer); }  void _splitnum(int * buffer, const int x, const size_t pos) { 	if (x <= 1) 	{ 		buffer[pos + 1] = x; 		buffer[pos + 2] = 0; 		WriteBuffer(buffer); 	} 	else 	{ 		size_t npos = pos + 1; 		for (int i = 1; i <= MIN(x, buffer[pos]); i++) 		{ 			buffer[npos] = i; 			_splitnum(buffer, x - i, npos); 		}                buffer[npos] = x;           //修改了下                buffer[npos+1] = 0;                WriteBuffer(buffer); 	} }  void SplitNum(const int x) { 	if (x <= 0) return; 	int * buffer = (int*)malloc(sizeof(int) * (x + 1)); 	for (int i = 1; i < x; i++) 	{ 		buffer[0] = i; 		_splitnum(buffer, x - i, 0); 	} }  int main() { 	int x = 0; 	if (scanf("%d", &x)) 		SplitNum(x);     return EXIT_SUCCESS; }  修改了些，试试[/quote]

user: user-4
content: 引用 1 楼 iyomumx 的回复:仅供参考 #include <stdio.h> #include <stdlib.h>  #define MIN(X,Y) (((X)<(Y))?(X):(Y))  void WriteBuffer(const int * buffer) { 	while (buffer[1] != 0) 	{ 		printf("%d+", *buffer++); 	} 	printf("%d\n", *buffer); }  void _splitnum(int * buffer, const int x, const size_t pos) { 	if (x <= 1) 	{ 		buffer[pos + 1] = x; 		buffer[pos + 2] = 0; 		WriteBuffer(buffer); 	} 	else 	{ 		size_t npos = pos + 1; 		for (int i = 1; i <= MIN(x, buffer[pos]); i++) 		{ 			buffer[npos] = i; 			_splitnum(buffer, x - i, npos); 		}                buffer[npos] = x;           //修改了下                buffer[npos+1] = 0;                WriteBuffer(buffer); 	} }  void SplitNum(const int x) { 	if (x <= 0) return; 	int * buffer = (int*)malloc(sizeof(int) * (x + 1)); 	for (int i = 1; i < x; i++) 	{ 		buffer[0] = i; 		_splitnum(buffer, x - i, 0); 	} }  int main() { 	int x = 0; 	if (scanf("%d", &x)) 		SplitNum(x);     return EXIT_SUCCESS; }  修改了些，试试

user: user-0
content: 引用 1 楼 iyomumx 的回复:仅供参考 #include <stdio.h> #include <stdlib.h>  #define MIN(X,Y) (((X)<(Y))?(X):(Y))  void WriteBuffer(const int * buffer) { 	while (buffer[1] != 0) 	{ 		printf("%d+", *buffer++); 	} 	printf("%d\n", *buffer); }  void _splitnum(int * buffer, const int x, const size_t pos) { 	if (x <= 1) 	{ 		buffer[pos + 1] = x; 		buffer[pos + 2] = 0; 		WriteBuffer(buffer); 	} 	else 	{ 		size_t npos = pos + 1; 		for (int i = 1; i <= MIN(x, buffer[pos]); i++) 		{ 			buffer[npos] = i; 			_splitnum(buffer, x - i, npos); 		} 	} }  void SplitNum(const int x) { 	if (x <= 0) return; 	int * buffer = (int*)malloc(sizeof(int) * (x + 1)); 	for (int i = 1; i < x; i++) 	{ 		buffer[0] = i; 		_splitnum(buffer, x - i, 0); 	} }  int main() { 	int x = 0; 	if (scanf("%d", &x)) 		SplitNum(x);     return EXIT_SUCCESS; }  谢谢

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:“给定一个小点的输入，完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门！ 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出 赵老师的回答太专业了 赞一个 先谢谢了 我好好学习学习下

user: user-0
content: 赵老师的回答太专业了 赞一个 先谢谢了 我好好学习学习下