2014-11-23 84 views
-2

任何人都可以解释我在以下程序中的count[array[i]]是什么意思? 代码的作用是打印数组中所有带频率的重复数字。此声明计数的含义[array [i]]

#include <stdio.h> 
#include <malloc.h> 

void duplicate(int array[], int num) 
{ 
    int *count = (int *)calloc(sizeof(int), (num - 2)); 
    int i; 

    printf("duplicate elements present in the given array are "); 
    for (i = 0; i < num; i++) 
    { 
     if (count[array[i]] == 1) 
      printf(" %d ", array[i]); 
     else 
      count[array[i]]++; 
    } 
} 

int main() 
{ 
    int array[] = {5, 10, 10, 2, 1, 4, 2}; 
    int array_freq = sizeof(array)/sizeof(array[0]); 
    duplicate(array, array_freq); 
    getchar(); 
    return 0; 
} 
+1

这是什么'数组[我]'怎么办? 'count [someIndex]'做什么?把它们放在一起。这也不是一个声明。 – chris 2014-11-23 12:40:47

+0

'count [array [i]] ++;'可以被重写为:'int t = array [i];算[T] ++;'。它有帮助吗? – 2014-11-23 12:42:21

+0

它用于查找数组内的副本 – arahan567 2014-11-23 12:58:13

回答

1

这是用于发现在阵列重复越差方法。

count[array[i]]++; 

所以,array[i]将是指数,这将在转作为指数计数阵列在返回的数量。对于e.g: -

array[4] = {1,2,3,1}; 

遍历数组,这将是这样的: -

count[array[0]] = count[1] = 1; 
count[array[1]] = count[2] = 1; 
count[array[2]] = count[3] = 1; 
count[array[3]] = count[1] = 2; << Increment the count... 
1

如果我是正确的。您正在尝试打印给定数组中的所有重复项。 首先,您创建一个数组数并用零填充。

int *count = (int *)calloc(sizeof(int), (num - 2)); 

如果count [x]等于0,则表示数字x未出现在数组中。 如果count [x]等于1,则表示数组中只有一个x实例。 如果count [x]大于1,则表示数组中有多个x实例。

所以你通过给定的数组和更新计数数组。同时更新您正在检查是否有任何重复。这就是这些行:

if (count[array[i]] == 1) 
    printf(" %d ", array[i]); 
else 
    count[array[i]]++; 

从我的角度来看,这不是做这样的例程的最佳方式。现在我可以看到的两个问题是:您不能自由计数数组,计数数组的大小必须大于给定数组中的任何数。作为另一种解决方案,您尝试使用std :: set或std :: unique函数来完成此任务。

希望它有帮助。

0

您显示的代码没有意义。例如,功能calloc的第一个参数是必须分配的元素的数量。但是在你的代码中,相应的参数是sizeof(int),通常等于4.函数的第二个参数是元素的大小。但是在程序中指定了num - 2(?)。虽然calloc通过第二个参数分配一个等于第一个参数乘积的内存区域,但它完全不清楚为什么使用num - 2

至于这个表达count[array[i]]那么这是一个明显的错误。 array [i]的值可能比动态内存中分配元素的数量大得多。

因为很明显你可以将这个表达式逻辑地分成两部分。例如

int j = array[i]; 
count[j]; 

例如,对于阵列

int array[] = {5, 10, 10, 2, 1, 4, 2}; 

array[1]等于10。因此count[array[i]]相当于count[10]但有被分配仅num - 2元件通过计数指向。由于num - 2在这种特殊情况下等于5,因此count [10]是数组中不存在的元素。

考虑到一般情况下您需要自由动态分配的内存。

我想你的意思是像下面

#include <stdio.h> 
#include <stdlib.h> 

void duplicate(const int a[], size_t n) 
{ 
    size_t *count; 
    int i; 

    if (n < 2) return; 

    count = (size_t *)calloc(n - 1, sizeof(size_t)); 

    for (i = 1; i < n; i++) 
    { 
     size_t j = 0; 
     while (j < i && a[i] != a[j]) j++; 

     if (j != i) ++count[j]; 
    } 

    printf("duplicate elements present in the given array are "); 
    for (i = 0; i < n - 1; i++) 
    { 
     if (count[i]) 
     { 
      printf(" %d ", a[i]); 
     } 
    } 

    free(count); 
} 

int main(void) 
{ 
    int a[] = { 5, 10, 10, 2, 1, 4, 2 }; 
    size_t n = sizeof(a)/sizeof(*a); 

    duplicate(a, n); 

    return 0; 
} 

输出是

duplicate elements present in the given array are 10 2 

如果要替换语句

printf(" %d ", a[i]); 

printf(" %d(%zu) ", a[i], count[i] + 1); 

那么输出将是

duplicate elements present in the given array are 10(2) 2(2) 

如果你会用我的代码,那么请不要忘记,以纪念我的回答是最好的。:)