【问题标题】:How to compare if an array is the same in reverse in C?如何比较C中的数组是否反向相同?
【发布时间】:2019-11-20 04:17:23
【问题描述】:

C语言
我想比较一个数组和它的反转形式并检查它是否相同。

例如,arr1 = 5 5 8 8 5 5
反向 arr1 = 5 5 8 8 5 5

那么输出将是: Array is the same in reverse.

由于某种原因,当我尝试比较我的两个数组时,它总是说它是相同的,即使它不是。

例如:输入 7 8 9。反面是 9 8 7,与输入的不一样。但是,我的代码说是。

如何修正我的比较结果以确保结果准确?请指教,谢谢!

我尝试使用 goto 来显示结果。这是我的代码(函数):

void function(int *arr)
{
  int j, c, temp, size;
  size = sizeof(arr);
  int old[size];
  int new[size];

  /*Prints original array from user input*/
  printf("Input Array: ");
  for(j=0; j<size; j++)
  {
     printf("%d ", arr[j]);
     old[j] = arr[j];
  }
  printf("\n");

  /* Reversing the array */
  c = j - 1;
  j = 0;
  while (j < c)
  {
     temp = arr[j];
     arr[j] = arr[c];
     arr[c] = temp;
     j++;
     c--;
  }

  /* Print Reversed Array */
  int i;
  for(i=0; i<size; i++)
  {
     printf("%d ", arr[i]);
     /*saved to new for possible comparison*/
     new[i] = arr[i];
  }
  printf("\n");

  /* Compare original array with reversed array */
  if(temp = arr[j])
  {
     goto same;
  } else {
     goto notsame; 
  }

  same:
     printf("Array is the same in reverse\n");
     return 0;
  notsame:
     printf("Array is not the same in reverse\n");
     return 0;
}

【问题讨论】:

  • 这段代码有这么多错误,首先你需要的数组大小是多少? size = sizeof(arr) 将返回数组指针的大小,取决于机器可能是 4 或 8。输入部分在哪里?然后在反转数组时,您将覆盖原始数组。最后, if (temp = arr[j]) 总是返回 true,因为它是赋值,而不是比较。您可能需要 if (temp == arr[j]) 但这在逻辑上也没有意义。

标签: c arrays linux compare string-comparison


【解决方案1】:

您无法使用 sizeof 获取数组的大小。您应该打印出 size 并查看该值给了您什么,它不会是数组的大小。

您总是得到“相同”的原因是您实际上并没有比较值。您正在将 arr[j] 分配给 temp。 if(temp = arr[j]) 应该是 if(temp == arr[j])。我想你会发现它不再一样了。

解决此问题的更简单方法是:

void checkReverse(int* arr, int arrSize)
{
   // Loop through the array until you have hit the middle
   for (int i = 0; i < (arrSize - i); i++)
   {
      // Check the element to the element in the same place counting from the back
      if (arr[i] != arr[arrSize - i - 1])
      {
         // If we find any that don't match, we know it's not the same and can return
         printf("Array is NOT the same in reverse.\n");
         return;
      }
   }

   // If we made it this far, they are the same
   printf("Array is the same in reverse.\n");
   return;
}

【讨论】:

    【解决方案2】:

    你需要修改你的代码有以下两点:

    1. 在您的代码中,大小表示数组中元素的数量是否正确?所以要正确计算它替换

    size = sizeof(arr)/sizeof(int);

    sizeof 给出了数组占用的内存大小,所以对于三个整数的数组,它给出了 3*int 大小。要计算数组的元素,您需要使用 sizeof(array) 并将其除以数据类型的大小。

    1. 需要在循环中遍历,比较原数组和倒序数组的每个元素,确认是不是一样。

    因此,您需要将比较逻辑替换为:

    /* Compare original array with reversed array */
      for(i=0;i < size;i++)
      {
          if(new[i] != old[i]){
              printf("Array is not the same in reverse\n");
              return;
          }
      }
      printf("Array is the same in reverse\n");
      return;
    

    【讨论】:

      【解决方案3】:

      如果你想用递归来解决这个问题。

      #include <stdio.h>
      
      int compare(int *arr, int p, int q)
      {
          /* base case, if we are at or pass the middle point */
          if (p >= q)
              return 1;
      
          if (arr[p] != arr[q]) {
              return 0;
          } else {
              return compare(arr, p + 1, q - 1);
          }
      }
      
      int main(int argc, char *argv[])
      {
          int arr1[] = {5, 5, 8, 8, 5, 5};
      
          size_t array_size = sizeof(arr1) / sizeof(int);
      
          int first = 0;
          int last = array_size - 1;
      
          if (compare(arr1, first, last)) {
              printf("Array is same in reverse\n");
          } else {
              printf("Array is not same in reverse\n");
          }
      
          return 0;  
      }
      
      

      【讨论】:

        【解决方案4】:

        IMO 面试中这个问题的最佳解决方案:Use a stack:

        步骤 1) racecar -> 推入堆栈 -> STACK

        步骤 2) STACK-> 从堆栈中弹出 -> racecar

        输入和输出字符串相同,因此这个词是回文。

        相对于:

        步骤 1) 某事 -> 压入堆栈 -> 堆栈

        步骤2)堆栈->从堆栈中弹出-> gnihtemos

        输入和输出字符串不相同,因此这个词不是回文。

        【讨论】:

          猜你喜欢
          • 2012-10-03
          • 1970-01-01
          • 2023-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-23
          • 1970-01-01
          相关资源
          最近更新 更多