C 计数数组值错误

C 计数数组值错误,c,algorithm,data-structures,C,Algorithm,Data Structures,您好,我需要帮助。我需要修复此代码,以便它只计算一次值 适用于exmaple 输入: 二十五 38 25 36 4 1 10 37 45 21 37 42 21 1 50 9 50 42 6 39 10 14 11 20 十, 36422152842323850 输出: 四, 这里的答案应该是4而不是7 #include <stdio.h> int main() { int n, m, count = 0; int array[1000]; int suba

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

适用于exmaple

输入:

二十五

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

十,

36422152842323850

输出:

四,

这里的答案应该是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);


}
#包括
int main()
{
整数n,m,计数=0;
整数数组[1000];
整数子阵[1000];
scanf(“%d”和“&n”);
对于(int i=0;i
您需要跟踪第三个数组中的匹配值,如下所示 检查是否在之前找到新值

#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);


}
#包括
int main()
{
整数n,m,计数=0;
整数数组[1000];
整数子阵[1000];
int结果[1000];
scanf(“%d”和“&n”);
对于(int i=0;i对于(int k=0;k我使用一个布尔变量来检查数组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);
}
#包括
#include//header如何找到booleen变量
int main()
{
int n,m;
做
{
printf(“给我数组1的长度:”);
scanf(“%d”和“&n”);

}而(n可能的解决方案,使用函数和qsort

(未测试代码)

#包括
int二进制搜索(int arr[],int l,int r,int x){
如果(r>=l){
int mid=l+(r-l)/2;
如果(arr[mid]==x)
中途返回;
如果(arr[mid]>x)
返回二进制搜索(arr,l,mid-1,x);
返回二进制搜索(arr,mid+1,r,x);
} 
返回-1;
}  
int cmpfunc(常数无效*a,常数无效*b){
返回(*(int*)a>*(int*)b)-(*(int*)a<*(int*)b);
}
int main(){
整数n,m,计数=0;
整数数组[1000];
整数子阵[1000];
scanf(“%d”和“&n”);
对于(int i=0;i
所以它只计算一次值

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

  • 检查
    子数组[]
    值是否已在其自身中出现

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


for(int j=0;j

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

// 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++;
}
//伪代码
使用qsort()对数组[n]进行排序
使用qsort()对子数组[m]进行排序
//遍历数组以查找匹配项
i=0;
j=0;
而(i
您的检查逻辑有缺陷,因为如果
n>m
,内部循环将超出
子数组的界限。使用调试器几分钟就会告诉您这一点。您可以添加问题和代码的目的吗?很难理解您正在尝试做什么。一个小于25和10的示例会更简单我的建议是,对第一个数组进行排序,删除连续的重复项,然后使用二进制搜索从“子数组”中查找每个数字。您可能还应该对“子数组”中的重复项进行排序并删除它们。(排序是二进制搜索工作所必需的,并且使删除重复项更加容易。)@有些程序员在数组排序后,不需要删除重复项,只需在比较过程中跳过重复项,因为它们很容易找到,因为它们是相邻的。
// 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++;
}