c - 相等运算检查数组中的四个值是否相等,这些值必须依次排列

标签 c

此函数应返回第一个值等于下一个值时的 k,依此类推。 该函数当前给我 0 作为输出。

int function(double array1[])
{
    int k = 0, count = 0;

    for( k = 0; k < 720; k++ )
    {
        if( ( (array1[k]   != 0) && 
              (array1[k+1] != 0)) && 
            ( (array1[k+2] != 0) && 
              (array1[k+3] != 0) ) )
        {
            if( ( (array1[k]) == 
                  (array1[k+1]) ) == 
                ( (array1[k+2]) ==
                  (array1[k+3]) ) )
            {
                count++ ;
            }
        }
    }

    return count;
}

最佳答案

首先,你需要给你的代码一些结构,这是一个例子

int compare(const int *const array, size_t k, size_t size)
{
    for (int j = k + 1; ((j < k + 4) && (j < size)); ++j) {
        // If a signle value is not equal to
        // `array[k]' we immediately return
        // false. 
        if (array[k] != array[j]) {
            return 0;
        }
    }
    return 1;    
}

int function(const int *const array, size_t size)
{
    int count = 0;
    for (int k = 1; k < size; k++) {
        // If array[k - 1] == 0, then it doesn't matter if the following
        // three values are equal to it, so we skip this one
        if (array[k - 1] == 0) {
            continue;
        }

        // Check that all the following three values are equal to
        // `array[k - 1]'
        if (compare(array, k - 1, size) != 0) {
            count += 1;
        }
    }
    // Now this could be the expected value
    return count;
}

另外,请注意我所做的一些更改

  1. 我将 double 更改为 int,因为如果有的话,比较 double 值是否相等是没有意义的,因为浮点精度。

  2. 我现在,检查感兴趣值后面的所有 3 个值在不同的函数中是否相等,这使得代码清晰,任何阅读它的人都明白它在做什么。

  3. 我将 int array[] 更改为 int *array 因为如果有的话 int array[] 只会让您感到困惑不是专家,如果您是专家,您就知道无论语法如何,array 都是一个指针。

  4. 添加了 2 个 const 限定符,

    1. 第一个,因为我们可以确保这些函数不会修改数组,而您应该这样做。

    2. 第二个,因为我们不想意外地重新评估函数内的指针,也不想意外地增加或改变它。

  5. 添加了数组大小作为参数,使得函数可以重用,这是函数非常重要的特性。

  6. 添加了注释来解释代码的作用。请注意,这些注释的目的不仅仅是“了解代码的作用”,因为从代码本身就可以清楚地看出这一点,正是因为这样您可以看到您认为的内容之间是否存在一致性代码将执行的操作以及它实际执行的操作。

如果你想要一个稍微好一点的版本,这个也可以

int compare(const int *const array, size_t size, size_t width)
{
    for (const int *next = array + 1; next < array + width; ++next) {
        // If a signle value is not equal to
        // `array[0]' we immediately return
        // false. 
        if (array[0] != next[0]) {
            return 0;
        }
    }
    return 1;    
}

int function(const int *const array, size_t size, size_t width)
{
    int count = 0;
    for (int k = 1; k < size; k++) {
        // If array[k - 1] == 0, then it doesn't matter if the 
        // following three values are equal to it, so we skip this
        // one
        if (array[k - 1] == 0) {
            continue;
        }    
        // Check that all the following three values are equal to
        // `array[k - 1]'
        if (compare(&array[k - 1], size, width) != 0) {
            count += 1;
        }
    }
    return count;
}

它比前一个没有多大改进,但它多了一个参数可调,因此功能更通用,因此更“可重用”。

关于c - 相等运算检查数组中的四个值是否相等,这些值必须依次排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573257/

相关文章:

c - 在不多次添加角的情况下对矩阵的周长求和

c - 为什么使用GDB检查时,某些局部变量没有列在相应的堆栈帧中?

c - 来自 recvfrom 的以太网 header 与预期不符

c - 将具有不同变量类型的文件读入c中的结构

用 GCC 编译 C 文件会产生错误?

c - K&R 1.6阵列。不懂代码

c - 分配给结构的非 const 成员时出现 "assignment of read-only member"错误

java - 多核编程 : what's necessary to do it?

c - 错误 C2440 : 'function' : cannot convert from 'const IID' to 'DWORD'

c++ - 将成员函数传递给需要回调的 C 接口(interface)