小编Omr*_*lka的帖子

递归-数据结构课程-打印所有可能的系列

我需要打印所有可能的序列,它们的总和等于N;例如n == 4,输出应为:

[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[1, 3]
[2, 1, 1]
[2, 2]
[3, 1]
[4]
Run Code Online (Sandbox Code Playgroud)

我想解决这个问题的方式是:打印出序列中编号i不在的序列,打印出序列中编号i的序列,现在需要找到Ni的总和。

我的代码:

#include <stdio.h>
#include <stdlib.h>

void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {

        printf(" %d ", arr[i]);
    }
    printf("\n");

}

void printAllHelper(int* a,int size, int sum, int used,int index) {
    if (sum == 0) {
        a -= used;
        printArr(a, used);
    }
    else if (sum < 0 || index …
Run Code Online (Sandbox Code Playgroud)

c algorithm recursion series data-structures

4
推荐指数
1
解决办法
62
查看次数

标签 统计

algorithm ×1

c ×1

data-structures ×1

recursion ×1

series ×1