递归子长度 - 升序整数的最长序列

时间:2017-09-11 13:45:14

标签: c algorithm recursion substring

我正在尝试编写一个递归函数,它接收一个数组及其大小和其他一些变量(我选择的)。该函数返回数组中升序整数的最长序列的长度。限制不是使用循环而不是调用其他函数或使用指针或其他库。

这是我写的代码,遗憾的是它不起作用,我不明白为什么!

length4 = max_set(arr4, size4, 1, 0, 1, 1, 0);

int max_set(int arr[], int size, int max, int index1, int index2, int 
currentMax, int i) {
 int path1, path2;
 if (index1 == size)
     return currentMax;
 if (index2 == size){
     if (currentMax < max)
         currentMax = max;
     index1 = i + 1;
     index2 = index1 + 1;
     max = 1;
 }
 if (arr[index2]> arr[index1]){
     path1 = max_set(arr, size, max + 1, index1, index2 + 1, currentMax, i);
     path2 = max_set(arr, size, max + 1, index1 + 1, index2 + 1, currentMax, i);
 }
 path1 = max_set(arr, size, max, index1, index2 + 1, currentMax, i);
 path2 = max_set(arr, size, max, index1 + 1, index2 + 1, currentMax,i);
 if (path1 > path2)
    return path1;
 else
     return path2;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现原始代码比手头的任务更复杂,所以我只是简单地简化了它,而不是尝试修复它:

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

/*
 * Determine maximum ascending sequence length using length so far.
 * @arr points to first element of array.
 * @size is length of array.
 * @pos is position of remaining entries in array.
 * @maxval is the maximum value included the sequence so far (if len > 0).
 * @len is sequence length so far.
 *
 * Returns maximum ascending sequence length.
 */
int max_set_recurse(int arr[], int size, int pos, int maxval, int len)
{
    int path1, path2, newlen;

    if (size <= pos) {
        /* Nothing left, so return sequence length so far. */
        return len;
    }
    /* Try skipping the first remaining entry. */
    path1 = max_set_recurse(arr, size, pos + 1, maxval, len);
    if (len && arr[pos] <= maxval) {
        /* First remaining entry is no larger than maximum value. */
        /* Try beginning sequence at first remaining entry. */
        newlen = 0;
    } else {
        /* Try extending the sequence with first remaining entry. */
        newlen = len + 1;
    }
    path2 = max_set_recurse(arr, size, pos + 1, arr[pos], newlen);
    /* Return the longest ascending sequence length of the two we tried. */
    return path2 > path1 ? path2 : path1;
}

/*
 * Determine maximum ascending sequence length
 * @arr points to entries.
 * @size is number of entries.
 *
 * Returns maximum ascending sequence length.
 */
int max_set(int arr[], int size)
{
    return max_set_recurse(arr, size, 0, 0, 0);
}

int main(int argc, char *argv[])
{
    int arr[argc - 1];
    int i;

    for (i = 1; i < argc; i++) {
        arr[i - 1] = atoi(argv[i]);
    }
    printf("Longest ascending sequence length = %d\n", max_set(arr, argc - 1));
    return 0;
}

EDITED 遵守&#34;没有指针&#34;尽可能在C中规则,即没有明显使用指针,因为数组是作为指针传递,数组索引实际上是伪装的指针算术和指针解引用!)

调用max_set_recurse函数,要检查数组的剩余部分(从pos开始),到目前为止的部分最大升序中的最大值(如果有的话),以及到目前为止,部分最大上升序列的长度(可能为零)。

  • 如果数组的剩余部分为空(size <= pos),则只返回序列的长度。
  • 否则,我们可以确定两个序列长度,一个排除数组中第一个剩余条目(arr[pos]),另一个包含它。该函数将返回这两个序列长度的最大值。
  • 对于排除数组中第一个剩余条目的递归路径,递归调用将跳过第一个剩余条目并传递先前的最大值和序列长度。
  • 对于包含数组中第一个剩余条目的递归路径,递归调用将跳过第一个剩余条目,使用第一个剩余条目的值作为最大值,并按如下方式设置序列长度:
    • 如果前一个长度为零,或者第一个剩余条目大于前一个最大值,则使用前一个序列长度加一;
    • 否则,使用序列长度为零。

在命令行上使用一系列数字调用演示程序。它将打印通过跳过或包含每个元素可以找到的最长升序的长度。

以下是一些示例命令行和输出。我已经在粗体中标记了构成最长序列一部分的输入数字,尽管该程序没有报告哪些数字是最长升序列的一部分:

./上升 3 23 2 45 0 21 80
Longest ascending sequence length = 4
./asce 10 3 5 23 2 45 0 21 80 Longest ascending sequence length = 5
./asce 10 3 5 23 2 45 21 80 20 21 22 <强> 23
Longest ascending sequence length = 6

相关问题