存在相同数字的最长序列和最多的最长序列(没有数组)

时间:2018-03-20 16:26:38

标签: c algorithm loops do-while

在C中,我只允许使用stdio.h而且我不允许使用数组。

问题是:我需要编写函数" void MaxLegnth()" 输入整数只要它们是正数,如果输入负数,输入部分将停止。 我需要找到相同数字的最长序列,以及它发生了多少次。例如:

输入:

19 19 97 97 97 97 681 681 681 681 97 36 36 36 97 97 97 97 36 -19     

输出:

maximum length - 4  
occurred - 3  

我一直在努力解决这个问题并想出了这个问题,但我无法弄清楚如何解决这个问题:

void MaxLength()
{
    int num1, temp, currentmax = 0, countmax = 0, globalmax = 0;
    printf("Please enter positive integers: ");
    scanf_s("%d", &num1);
    temp = num1;
    if (num1 > 0)
        currentmax++;
    while (num1 > 0)
    {
        scanf_s("%d", &num1);
        if (num1 < 0)
        {
            //currentmax--;
            break;
        }
        if (num1 == temp)
        {
            currentmax++;
            temp = num1;
            if (currentmax > globalmax)
                globalmax = currentmax;
        }
        else
        {
            if (currentmax > globalmax)
            {
                globalmax = currentmax;
                countmax = 1;
                currentmax = 1;
                temp = num1;
            }
            if (currentmax < globalmax)
            {
                currentmax = 1;
                temp = num1;
            }
            if (currentmax == globalmax)
                countmax++;
        }

    }
    printf("\ncurrent %d\n", currentmax);
    printf("\nglobal %d\n", globalmax);
    printf("\ncount %d\n", countmax);
}

抱歉,这是一面巨大的文字墙。任何帮助将非常感谢!

3 个答案:

答案 0 :(得分:0)

以下是您的函数的修复版本,其中包含一些注释:

int num = 0, curr = -1, currCnt = 0, maxCnt = 0, maxCntCnt = 0;
printf("Please enter positive integers: ");
while (num >= 0) {
    if (scanf("%d", &num) != 1) 
        break;
    if (num <= 0)
        break;
    if (num == curr) {
        currCnt++; // new number is continuation of sequence, increase counter
        if (currCnt == maxCnt) {
            maxCntCnt++; // we're reached previously detected max, increase counter
        }
        if (currCnt > maxCnt) {
            maxCnt = currCnt; // we're reached new max, reset counter
            maxCntCnt = 1;
        }
    } else {
        curr = num; // start of new sequence detected, reset counter
        currCnt = 1;
    }
}

printf("\nmaximum length - %d\n", maxCnt);
printf("occured - %d\n", maxCntCnt);

答案 1 :(得分:0)

我们初学者应该互相帮助。:)

首先考虑到声明该函数(当声明也是一个函数定义时),如

void MaxLength( void )
                ^^^^^
{
    //...
}

你的功能太复杂了。

这是我建议的解决方案。

#include <stdio.h>

void MaxLength( void )
{
    size_t max_length = 0, max_count = 0;
    int prev_number = 0;

    size_t n = 0;
    _Bool valid_input;

    printf( "Please enter positive integers: " );

    do
    {
        int number = 0;

        valid_input = scanf( "%d", &number ) == 1 && number > 0;

        if ( !valid_input || number != prev_number )
        {
            if ( max_length < n )
            {
                max_length = n;
                max_count  = 1;
            }
            else if ( valid_input && max_length == n )
            {
                max_count++;
            }

            prev_number = number;
            n = 1;
        }
        else
        {
            ++n;
        }

    } while ( valid_input );

    printf( "\nmaximum length - %zu\n", max_length );
    printf( "occured - %zu\n", max_count ); 
}

int main(void) 
{
    MaxLength();

    return 0;
}

程序输出

Please enter positive integers: 19 19 97 97 97 97 681 681 681 681 97 36 36 36 97 97 97 97 36 -19

maximum length - 4
occured - 3

答案 2 :(得分:0)

因为每个人都给出答案,这是我的:

void MaxLength(void)
{
    int inputNumber;
    int previousNumber = -1;
    int currentLen     = 0;
    int maxLen         = 0;
    int nbMaxLen       = 0; 

    printf("Please enter positive integers: ");
    while (scanf("%d", &inputNumber) == 1 && inputNumber > 0) {
        ++currentLen;
        if (inputNumber != previousNumber) {
            if (currentLen == maxLen) {
                ++nbMaxLen;
            } else if (currentLen > maxLen) {
                maxLen = currentLen;
                nbMaxLen = 1;
            }
            currentLen = 1;
        }
        previousNumber = inputNumber;
    }

    if (currentLen == maxLen) {
        ++nbMaxLen;
    } else if (currentLen > maxLen) {
        maxLen = currentLen;
        nbMaxLen = 1;
    }

    printf("maxLen   = %d\n", maxLen);
    printf("nbMaxLen = %d\n", nbMaxLen);
}

你的问题来自于while循环中存在轻微错误,例如如果currentmax == globalmax,则temp1将不会获取num1值,并且我不认为这是应该做的。

在全球范围内,我们使用相同的算法,但如果您发现了可疑的内容,请随时提出问题。