比如6=1+5 6=2+4 6=3+3 6=1+1+4 ...
直接上代码:(因为课是C语言课,就不用c++了,栈写起来麻烦,直接用数组换了)
#include <stdio.h>
#include <memory.h>
#define MAXSTACK 100
unsigned int string[MAXSTACK] = { 0 };
unsigned int pos = 1;//NEXT POSITION
unsigned int number;
void divide(int n)
{
if (n <= 1){
return;
}
int i = 1;
for (i=n/2; i>=string[pos-1]&&i>=1; i--)
{
unsigned int j;
printf("%d=", number);
for (j = 1; j < pos; j++)
printf("%d+", string[j]);
printf("%d+%d\n", i, n

本文介绍了一种使用C语言实现正整数加法分解的方法,通过递归算法将整数拆分为不同组合。代码中,定义了一个数组模拟栈来存储拆分的数字,然后遍历寻找合适的加数进行输出。例如,6可以拆分为1+5,2+4,3+3等。程序读取用户输入的正整数并进行分解,直到用户输入0为止。
1万+

被折叠的 条评论
为什么被折叠?



