题目:输入一个数,找出它的所有递减序列,如输入5,则输出
5
4 1
3 2
3 11
2 2 1
2 1 1 1
1 1 1 1 1
思想:递归求解,条件判断走向和输出
程序实现:
#include<stdio.h>
#include<stdlib.h>
void fun(int * seeds, int k, int count, int max)
{
int i;
if(k==0){
for(i = count-1;i >=0;i--){
printf("%d ", seeds[i]);
}
printf("\n");
return;
}
for(i = k;i >=max;i--){
seeds[count] = i;
fun(seeds, k - i, count+1, i);
}
}
int main(void)
{
printf("Please input the value:");
int num;
scanf("%d", &num);
int* seeds =(int *)malloc(sizeof(int) * num);
fun(seeds, num, 0, 1);
free(seeds);
return 0;
}