C语言题 编程
来源:百度知道 编辑:UC知道 时间:2024/06/21 23:05:20
用C语言实现整数分解(不重复)如: 5
1+4 2+3 1+1+3 1+2+2 1+1+1+2 1+1+1+1+1
1+4 2+3 1+1+3 1+2+2 1+1+1+2 1+1+1+1+1
#include "stdio.h"
void main()
{
int n;
scanf("%d", &n);
if (n == 1)
{
printf("1=1\n");
return;
}
if (n == 2)
{
printf("2=1+1\n");
return;
}
int *a = new int[n];
int top = 0;
a[0] = n - 1;
a[1] = 1;
top = 2;
int i;
do{
printf("%d=%d", n, a[0]);
for (i = 1; i < top; i++)
{
printf("+%d", a[i]);
}
printf("\n");
int s = 0;
do{
s += a[--top];
}while (top >= 0 && a[top] == 1);
if (top == -1)
{
break;
}
int d = a[top] - 1;
if (d == 1)
{
while (s > 0)
{
a[top++] = 1;
s--;
}
}
else
{
do{
a[top++] = d;
s -= d;
}while (s