小编And*_*ncă的帖子

计算交替的上/下序列

问题描述:计算从某个输入n向上的所有序列的数量.所以用户输入n; 然后我创建一个数字1..n数组,然后用该属性编号序列

例: n = 4

1 3 2 4
1 4 2 3
2 3 1 4
2 4 1 3
3 4 1 2
Run Code Online (Sandbox Code Playgroud)

回答: 5

我的程序有效,但出于某种原因,我有时会得到0而不是答案.

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

void *safeMalloc(int n) {
    void *p = malloc(n);
    if (p == NULL) {
        printf("Error: malloc(%d) failed. Out of memory?\n", n);
        exit(EXIT_FAILURE);
    }
    return p;
}

void swap(int *fir, int *sec) {
    int temp = *fir;
    *fir = *sec;
    *sec = temp;
}

void permute(int *array, int i, …
Run Code Online (Sandbox Code Playgroud)

c algorithm recursion

5
推荐指数
1
解决办法
212
查看次数

标签 统计

algorithm ×1

c ×1

recursion ×1