【问题标题】:count array value bug计数数组值错误
【发布时间】:2021-03-25 19:35:05
【问题描述】:

您好,我需要帮助,我需要修复此代码,以便它只计算一次值

例如

输入:

25

38 25 36 4 1 1 10 37 45 21 37 42 21 1 50 9 50 42 6 39 10 14 17 11 20

10

36 42 2 15 28 42 3 23 8 50

输出:

4

这里的答案应该是 4 而不是 7。

#include <stdio.h>

int main()
{
    int n, m, count = 0;
    int array[1000];
    int subarray[1000];

    scanf("%d", &n);

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }

    scanf("%d", &m);

    for (int i = 0; i < m; i++)
    {
        scanf("%d", &subarray[i]);
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (array[i] == subarray[j])
                count++;
        }
    }

    printf("%d\n", count);


}

【问题讨论】:

  • 您的检查逻辑有缺陷,因为如果n &gt; m,内部循环将超出subarray 的范围。使用调试器几分钟就会告诉你这一点。
  • 你能添加代码的问题和目的吗?很难理解您要做什么。
  • 一个小于 25 和 10 的例子会更容易帮助
  • 我的建议是你对第一个数组进行排序,去除连续的重复项,然后使用二分查找从“子数组”中查找每个数字。您也应该对“子数组”中的重复项进行排序和删除。 (二分搜索需要排序才能工作,也使得删除重复项更容易。)
  • @Someprogrammerdude 数组排序后,无需删除重复项,只需在比较过程中跳过重复项,因为它们相邻,因此很容易找到。

标签: c algorithm data-structures


【解决方案1】:

可能的解决方案,使用函数和 qsort。

(未经测试的代码)

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x) { 
    if (r >= l) { 
        int mid = l + (r - l) / 2; 
        if (arr[mid] == x) 
            return mid; 
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 
        return binarySearch(arr, mid + 1, r, x); 
    } 
    return -1; 
}  

int cmpfunc (const void * a, const void * b) {
   return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}

int main() {
    int n, m, count = 0;
    int array[1000];
    int subarray[1000];

    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }

    qsort(array, n, sizeof(int), cmpfunc); // O(n lg n)

    scanf("%d", &m);

    for (int i = 0; i < m; i++) {
        scanf("%d", &subarray[i]);
        int result = binarySearch(arr, 0, n - 1, x); // O(lg n)
        if (result != -1)
          count++;
    } // O(m lg n)
    
    printf("%d\n", count);
}

【讨论】:

    【解决方案2】:

    所以它只计算一次值

    对 OP 代码的影响最小且无需重新排序输入:

    • 检查subarray[] 值是否已在其内部出现。

    • subarray[]/array[] 值匹配时,停止查找。


    for (int j = 0; j < m; j++) {
      bool unique_subarray_value = true;
      for (int earlier = 0; earlier < j; earlier++) {
        if (subarray[earlier] == subarray[j])
          unique_subarray_value = false;
          break;
        }
      }
      if (unique_subarray_value) {
        for (int i = 0; i < n; i++) {
          if (array[i] == subarray[j]) {
            count++;
            break;
          }
        }
      }
    }
    

    一种快速的方法是对数组进行排序,然后遍历它们

    // Pseudo code
    sort array[n]    with qsort()
    sort subarray[m] with qsort()
    
    // walk the arrays looking for matches
    i = 0;
    j = 0;
    while (i < n && j < m) {
      if (array[i] == subarray[j]) {
        count++;
        while (i + 1 < n && array[i] == array[i+1]) i++;
        while (j + 1 < m && subarray[j] == subarray[j+1]) j++;
      }
      if (array[i] < subarray[j]) i++;
      else j++;
    }
    

    【讨论】:

      【解决方案3】:

      我使用一个布尔变量来检查数组1的元素是否存在于数组2中,如果不存在,我将它复制到数组3中

      #include <stdio.h>
      #include <stdbool.h>//header how found the booleen variable
      
      int main()
      {
           int n,m;
           do
           {
               printf("Give me the length of array 1 :");
               scanf("%d",&n);
           }while(n<1);
      
           int T1[n];//declaration of Array 1
           do
           {
               printf("Give me the length of array 2 :");
               scanf("%d",&m);
           }while(m<1);
      
           int T2[m];//declaration of Array 2
      
           printf("The fill of Array 1:\n\n");
      
           for(int i=0;i<n;i++)
           {
               scanf("%d",&T1[i]);
           }
      
           printf("The fill of Array 2:\n\n");
      
           for(int j=0;j<m;j++)
           {
               scanf("%d",&T2[j]);
           }
           int T3[n+m];//declaration of Array 3
      
           bool found=false;//declaration of booleen variable
           int k=0;
        
           for(int i=0;i<n;i++)
           {found=false;
              for(int j=0;j<m;j++)
              {
                    if(T1[i]==T2[j])
                    {
                         found=true;
                    }
              }
               if(found==true)
               {
                   T3[k]=T1[i];
                   k++;
               }
           }
           printf("\nThe number of elment duplicate is :%d\n",k);
      }
      

      【讨论】:

        【解决方案4】:

        您需要跟踪第三个数组中的匹配值,如下所示 检查之前是否找到了新值

        #include <stdio.h>
        
        int main()
        {
         int n, m, count = 0;
         int array[1000];
         int subarray[1000];
         int result[1000];
         scanf("%d", &n);
        
         for (int i = 0; i < n; i++)
         {
            scanf("%d", &array[i]);
         }
        
         scanf("%d", &m);
        
         for (int i = 0; i < m; i++)
         {
            scanf("%d", &subarray[i]);
         }
        
         for (int i = 0; i < n; i++)
         {
            for (int j = 0; j < i; j++)
            {
                if (array[i] == subarray[j]){
                    int isFound=0;
                    for(int k=0;k<count;k++){
                        if(result[k]==array[i])
                            isFound=1;
                    }
                    if(isFound==0){
                        result[count]==array[i];
                        count++;
                    }
                }
            }
        }
        
        printf("%d\n", count);
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-03
          • 1970-01-01
          • 1970-01-01
          • 2019-02-12
          • 2012-07-09
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多