计算数字在数组中出现的次数

我正在研究一个小程序,它计算整数出现在数组中的次数。 我设法做到了这一点,但有一件事是我无法克服的。

我的代码是:

#include  int count_occur(int a[], int num_elements, int value); void print_array(int a[], int num_elements); void main(void) { int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4}; int num_occ, i; printf("\nArray:\n"); print_array(a, 20); for (i = 0; i<20; i++) { num_occ = count_occur(a, 20, a[i]); printf("The value %d was found %d times.\n", a[i], num_occ); } } int count_occur(int a[], int num_elements, int value) /* checks array a for number of occurrances of value */ { int i, count = 0; for (i = 0; i<num_elements; i++) { if (a[i] == value) { ++count; /* it was found */ } } return(count); } void print_array(int a[], int num_elements) { int i; for (i = 0; i<num_elements; i++) { printf("%d ", a[i]); } printf("\n"); } 

我的输出是:

 Array: 2 5 0 5 5 66 3 78 -4 -56 2 66 -4 -4 2 0 66 17 17 -4 The value 2 was found 3 times. The value 5 was found 3 times. The value 0 was found 2 times. The value 5 was found 3 times. The value 5 was found 3 times. The value 66 was found 3 times. The value 3 was found 1 times. The value 78 was found 1 times. The value -4 was found 4 times. The value -56 was found 1 times. The value 2 was found 3 times. The value 66 was found 3 times. The value -4 was found 4 times. The value -4 was found 4 times. The value 2 was found 3 times. The value 0 was found 2 times. The value 66 was found 3 times. The value 17 was found 2 times. The value 17 was found 2 times. The value -4 was found 4 times. 

如何避免输出中的双线?

您可以使用并行数组,此示例使用char[20]以节省一些空间:

 #include  int count_occur(int a[], char exists[], int num_elements, int value); void print_array(int a[], int num_elements); int main(void) /* int main(void), please */ { int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4}; char exists[20] = {0}; /* initialize all elements to 0 */ int num_occ, i; printf("\nArray:\n"); print_array(a, 20); for (i = 0; i < 20; i++) { num_occ = count_occur(a, exists, 20, a[i]); if (num_occ) { exists[i] = 1; /* first time, set to 1 */ printf("The value %d was found %d times.\n", a[i], num_occ); } } } int count_occur(int a[], char exists[], int num_elements, int value) /* checks array a for number of occurrances of value */ { int i, count = 0; for (i = 0; i < num_elements; i++) { if (a[i] == value) { if (exists[i] != 0) return 0; ++count; /* it was found */ } } return (count); } void print_array(int a[], int num_elements) { int i; for (i = 0; i 

这个方法更快,因为它跳过已经重新加入的值并从count_ocurr i开始迭代:

 #include  int count_occur(int a[], char map[], int num_elements, int start); void print_array(int a[], int num_elements); int main(void) { int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4}; char map[20] = {0}; int num_occ, i; printf("\nArray:\n"); print_array(a, 20); for (i = 0; i < 20; i++) { if (map[i] == 0) { num_occ = count_occur(a, map, 20, i); printf("The value %d was found %d times.\n", a[i], num_occ); } } } int count_occur(int a[], char map[], int num_elements, int start) /* checks array a for number of occurrances of value */ { int i, count = 0, value = a[start]; for (i = start; i < num_elements; i++) { if (a[i] == value) { map[i] = 1; ++count; /* it was found */ } } return (count); } void print_array(int a[], int num_elements) { int i; for (i = 0; i< num_elements; i++) { printf("%d ", a[i]); } printf("\n"); } 

如果当前索引也是第一次出现的数字的索引,我建议只打印语句。

count_occur里面,你有i中每个匹配的索引。 如果你将imain传递给count_occur ,你可以做一些事情,例如如果该值大于count_occuri则返回-1。 然后,如果你在main得到-1,请不要打印。

此外,您的算法可以更快。 您可以对数组的副本进行排序,以便有效地完成搜索,而不是每次都线性搜索数组。 (即使您使用一个数组进行索引而另一个数组进行搜索,它也会更快 – 并且仍以相同的顺序返回值。)

 #include  #include  #include  int count_occur(int a[], int num_elements, int value, bool selected[]); void print_array(int a[], int num_elements); int main(void){ int a[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4}; int size = sizeof(a)/sizeof(*a); bool ba[size]; memset(ba, 0, sizeof ba); int num_occ, i; printf("\nArray:\n"); print_array(a, size); for (i = 0; i 

改善不大

 int count_occur(int a[], int num_elements, int index, bool selected[]); num_occ = count_occur(a, 20, i, ba); int count_occur(int a[], int num_elements, int index, bool ba[]){ int i, count = 0; for (i = index; i 
 #include #include int main() { int arr[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4}; int arrSize = sizeof(arr)/sizeof(arr[0]); int tracker[20]; int i,j,k=0,l=0,count,exists=0; for (i=0;i 
 very simple logic to count how many time a digit apper #include int main() { int a,b,c,k[10]; int p[10]={0}; int bb[10]={0}; scanf("%d\n",&a); for(b=0;b0;b--) { for(c=b-1;c>=0;c--) { if((k[b]==k[c])&&(bb[c]==0)) { p[b]=p[b]+1; bb[c]=1; } } } for(c=0;c 

在你的function:

 int count_occur(int a[], int num_elements, int value) /* checks array a for number of occurrances of value */ { int i, count = 0; for (i = 0; i 

在main()中你可以编辑for循环:

 for (i = 0; i<20; i++) { if(a[i] != INFINITY) { num_occ = count_occur(a, 20, a[i]); printf("The value %d was found %d times.\n", a[i], num_occ); } }