将变量-1、0或1初始化为变量的功能是什么?

时间:2019-09-15 09:35:12

标签: c

我遇到了一个问题,不得不为其编写一些代码。问题的上下文如下:

“考虑此数据序列:'3 11 5 5 5 2 4 6 6 7 3 -8'。与前一个值相同的任何值均被视为CONSECUTIVE DUPLICATE。在此示例中,存在三个这样的值连续重复:第二个和第三个5s和第二个6。请注意,最后3个不是连续重复,因为它前面有一个7。

编写一些代码,该代码使用循环读取以负数结尾的非负整数序列。当代码退出循环时,它应打印遇到的连续重复项的数量。在上述情况下,该值为3。“

因此,经过几次尝试并在网上查看了对类似代码的引用后,我记下了代码。我遇到的问题是弄清楚为什么将重复的变量和secondNumber分配给特定值,为什么firstNumber不应分配任何变量。

#include<stdio.h>

{
    int firstNumber;
    int secondNumber = -8;
    int duplicates = 0;

do {
    scanf("%d", &firstNumber);
        if ( secondNumber == -8) {
            secondNumber = firstNumber;
        }
        else {
            if ( secondNumber == firstNumber )
                duplicates++;
            else
                secondNumber = firstNumber;
        }
} while(firstNumber > 0 );

    printf("%d", duplicates);
}

当然,我使用给定的数字运行了该程序,并且可以正常工作,但是我对将-8和0组合到整个代码中的方式感到头疼。

3 个答案:

答案 0 :(得分:-1)

终止编程操作的条件必须为负数。而不是-8,可以有任何负数,这只是迫使您输入第二个数字的键。关于零,我不会回答-请说明您对哪个零感兴趣。

答案 1 :(得分:-1)

好的,让我们回顾一下

  • 输入具有一组非负整数
  • 输入以负数结尾
    #include<stdio.h>

    {
        int firstNumber; //no need to initialize since it is read from user
        int secondNumber = -8; //the value has no meaning other then it is 
                               //negative, following the end input termination
                               //it is used for the first comparison, since it 
                               //is stated that the input ends with a negative 
                               //number we can assure that a negative number will 
                               //not produce a false positive

        int duplicates = 0; //this variable is used as a counter - it must be
                            //initialized, otherwise duplicates++ will yield
                            // unexpected behavior

    do {
        scanf("%d", &firstNumber);
            if ( secondNumber == -8) { //this condition will evaluate to true
                                       //only on the first iteration of the loop
                secondNumber = firstNumber;
            }
            else {
                if ( secondNumber == firstNumber )
                    duplicates++;
                else
                    secondNumber = firstNumber;
            }
    } while(firstNumber > 0 ); //this is actually a bug the work statement said 
                               //non-negative integer, 0 is a valid input value
        printf("%d", duplicates);
    }
  • 请考虑当列表具有0个值时发生什么情况,所以这仅仅是结束条件,即负数
  • 条件secondNumber == -8只会被评估为true,但是每次都会检查一次,也许代码可以得到改进

希望有帮助

答案 2 :(得分:-1)

没有显式初始化程序的局部非静态变量具有 undefined 值。因此,在将值显式分配之前读取该值时,需要进行初始化。

可以在不进行初始化的情况下声明,然后在需要读取值之前明确地分配一个值。例如:

int duplicates ;
duplicates = 0 ;

但是使用显式初始化(​​即在创建变量时分配值)会关闭该变量具有未定义值的窗口。这样可以避免在分配变量之前读取变量的错误。从这种意义上讲,初始化所有非静态局部变量通常被视为优良作法

代码要做的第一件事是 assign firstNumber(通过scanf()),因此不需要初始化,但不会造成危害,通常建议避免未定义行为。

secondNumber在赋值之前先经过测试 的值,因此必须有一个值。测试secondNumber == -8在第一次迭代时为true。 -8的使用似乎是任意的,可以使用任何负值,-1则更惯用。

再次为duplicates赋值之前,它已经被修改,并且是连续重复项的计数,因此必须初始化为零。

该问题没有说明序列的表示方式,并且可以一概而论:

int dupCount( int* seq )
{
    int duplicates = 0 ;
    for( int i = 1; seq[i] >= 0; i++ )
    {
        if( seq[i] == seq[i-1] )
        {
            duplicates++ ;
        }
    }

    return duplicates ;
}

用法示例:

int main()
{
    int seq[] = {3, 11, 5, 5, 5, 2, 4, 6, 6, 7, 3, -1} ;
    printf( "%d\n", dupCount( seq ) ) ;

    return 0;
}

如果指定了任意长度的用户输入并且只是从您的问题中省略了,则:

#include <stdio.h>

int inputDupCount()
{
    int inp = 0 ;
    scanf( "%d", &inp ) ;

    int duplicates = 0 ;
    while( inp > 0 )
    {
        int prev_inp = inp ;
        scanf( "%d", &inp ) ;

        if( inp == prev_inp )
        {
            duplicates++ ;
        }
    }

    return duplicates ;
}

用法示例:

int main()
{
    printf( "%d\n", inputDupCount() ) ;
    return 0;
}
相关问题