相同值的最长序列C,数组

时间:2016-02-18 13:30:38

标签: c arrays algorithm

我需要在数组中找到相同值的最长序列,并返回此序列的长度。例如,对于int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};,答案应为3,因为我们这里有四个四肢。但是,我的代码总是返回0,我不知道为什么:

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 0, len = 0;
    int i;

    for(i=0; i<n; i++)
    {
        if(tab[i+1] == tab[i])
        {
            len ++;
            if (len > previous_len)
            {
                previous_len = len;
            }
        }
        else
        {
            previous_len = len;
            len = 0;
        }
    }
    return len;
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};
    int n = 9;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}

修改

根据评论中的建议更改了一些错误,但结果现在为1:

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 1, len = 1;
    int i;

    for(i=0; i<n-1; i++)
    {
        if(tab[i+1] == tab[i])
        {
            len ++;
            printf("len = %d\n", len);

            if (len > previous_len)
            {
                previous_len = len;
            }
        }
        else
        {
            previous_len = len;
            len = 1;
        }
    }
    return len;
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};
    int n = 9;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}

修改

It seems ok now :

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 1, len = 1, max = 0;
    int i;

    if(n == 0)
    {
        return 0;
    }
    else
    {
        for(i=0; i<n-1; i++)
        {
            if(tab[i+1] == tab[i])
            {
                len ++;

                if (len > previous_len)
                {
                    previous_len = len;
                }
            }
            else
            {
                previous_len = len;
                len = 1;
            }

            if(len > max)
                max = len;
        }
        return max;
    }
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 2, 2, 1, 3};
    int n = 11;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}

3 个答案:

答案 0 :(得分:3)

你在这里

#include <stdio.h>

size_t longest_sequence( const int *a, size_t n )
{
    size_t len = 0;

    for ( const int *p = a; p != a + n; )
    {
        const int *q = p++;
        while ( p != a + n && *p == *q ) ++p;
        if ( len < ( size_t )( p - q ) ) len = p - q;
    }        

    return len;
}    

int main( void ) 
{
    int tab[] = { 2, 2, 4, 4, 4, 2, 2, 1, 3 };
    const size_t N = sizeof( tab ) / sizeof( *tab );

    size_t len = longest_sequence( tab, N );

    printf( "%zu\n", len );

    return 0;
}

程序输出

3

考虑到像往常一样,我的解决方案是最好的解决方案。:)

答案 1 :(得分:2)

当当前值等于下一个值时,你应该增加一个计数器变量,然后当条件为假时,你应该检查序列的长度与存储最长序列长度的另一个变量,如果它更少比新长度为其分配新值,重置当前序列长度并继续,否则只需重置当前序列长度并继续

int
longest_sequence(int *array, int count)
{
    int length;
    int longest;
    longest = length = 0;
    for (int i = 0 ; i < count - 1 ; i++)
    {
        if (array[i + 1] == array[i])
            length++;
        else
        {
             // length + 1 to count the last element too
            if (longest < length + 1)
                longest = length + 1;
            length = 0;
        }
    }
    return longest;
}

int
main()
{
    int array[] = {2, 2, 4, 4, 4, 5, 2, 2, 1, 3};
    int count = sizeof(array) / sizeof(array[0]);

    printf("The longest sequence has `%d' elements.\n", 
        longest_sequence(array, count));
    return 0;
}

答案 2 :(得分:0)

您的代码中存在这些问题:

 else
 {
    if (len > previous_len) // <<<< you are missing this test
      previous_len = len;

    len = 1;
 }
    return previous_len;    // <<<< return previous_len instead of len