C中数组中最长的序列

时间:2014-04-15 10:37:54

标签: c arrays

我想创建一个程序,它返回数组中增长最长的序列。

例如:

输入:1,2,3,2,6,2 输出:1,2,3

输入:4,3,1,2,4,6,4,1,5,3,7 输出:1,2,4,6

我设法将一个代码放在一起,但这只返回了第一个连续的,增加数字的序列:

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

int main() {
    int j = 0;
    int cou = 0; 
    int max = 0; 
    // c = final array; will contain the longest consecutive, increasing sequence of numbers
    int c[10];
    int n = 0;  
    int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};

    for (int i = 0; i < (sizeof(a)/sizeof(int)); ++i) {
        if (a[i+1] > a[i])
            ++cou;
        if (cou > max) {
            max = cou;
            c[j] = a[i];
            c[j+1] = a[i+1];
            j++;
        }
        if (j > n)  //finding the size of my final array
            n = j;
        else {
            cou = 0;
            j = 0;
        }
    }

    for (j = 0; j <= n; ++j) 
        printf("%d ",c[j]);

    return 0;
}

所以基本上,我想要最长的连续数字序列。

现在已经在这个问题上绞尽脑汁了很长一段时间,仍然没有设法将它打开。欢迎任何帮助。

4 个答案:

答案 0 :(得分:1)

您需要遍历数组,查找序列并比较它们的长度。所以,你需要记住以前要比较的序列。并且您无法动态地将结果复制到输出数组(如果您需要输出数组),因为您无法预测下一个序列的长度。我最好给你看一个例子。

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

int main()
{
int previous_len=0, start=0, c[10], len=0;  //c = final array; will contain the longest consecutive, increasing sequence of numbers
int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};
for (int i = 0; i < (sizeof(a)/sizeof(int)); ++i) {
    if(a[i+1] > a[i]) {
        len++;
        if (len > previous_len) { 
            previous_len=len;
            start=i+1-len;
        }
    } else {
        previous_len=len;       
        len=0;
    }
}
for(int i = 0; i <= previous_len; ++i) {
    c[i]=a[start+i]; //here you can copy data to output array, if you need it
    printf("%d ",c[i]); //you can output a[start+i] instead
}
return 0;

}

答案 1 :(得分:0)

似乎你错过了一些花括号:

if(a[i+1] > a[i])
{
    ++cou;
    if (cou>max)
        {max = cou;
        c[j]=a[i];
        c[j+1] = a[i+1];
        j++;}
    if (j > n)  //finding the size of my final array
        n=j;
}
else
    {cou = 0;
     j=0;}

答案 2 :(得分:0)

我建议把它分成小块。

从功能开始:

int sequenceLength(int[] array, int arrayLen, int position)

...返回从position开始的序列长度。测试并确保 有用。你不应该需要帮助来写这个。

一旦你拥有了这个,你可以写下这样的东西:

int longestSequence(int[] array, int arrayLen) {
   int longest = 0;
   int longestLen = 0;
   for(int i=0; i<arrayLen; i++) {
       int seqLen = sequenceLength(array, arrayLen, i);
       if(seqLen > longestLen) {
           longest = i;
           longestLen = seqLen;
       }
   }
   return longest;
}

再次,测试一下并确保它适用于所有情况。

最后你需要一个功能:

printSequence(int[] array, int arrayLen, int position)

...打印从该位置开始的序列。你应该能够自己解决这个问题。

把所有这些放在一起:

printSequence(array,arrayLen(longestSequence(array,arrayLen)));

将这样的挑战分解为更小的部分来解决它总是最容易的。

可能有更有效的解决方案可以避免回溯,但猜测你的水平,我认为你不需要去那里。

(注意:虽然这里的代码可以编译,但可以将其视为伪代码)

答案 3 :(得分:0)

您使用数组存储最长的序列,这使您的代码在打印时出错 并且你使用大括号来导致错误序列的if()语句 您可以进行以下更改以使代码有效,

int main()
{int j=0, cou=0, max=0, c[10], n=0;  //c = final array; will contain the longest consecutive, increasing sequence of numbers
int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};
int i,k,z;
for ( k=0,i = 0; i < (sizeof(a)/sizeof(int)); ++i)
{if(a[i+1] > a[i])
  {  ++cou;
    if (cou>max)
        {max = cou;
        z=k;
        }

   }
    else
        {
            k=i+1;
            cou = 0;
         j=0;}
}
for( j = z; j <(sizeof(a)/sizeof(int)) ; ++j)
if(a[j]<a[j+1])
printf("%d ",a[j]);
else
 break;
printf("%d",a[j]);
return 0;

}