1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 void max_appear(int a[], int len); 5 6 int main() 7 { 8 9 int a[10000] = {2, 3, 4, 2, 2, 5, 3, 4, 2, 4}; 10 int i = 0; 11 int len = 0; 12 srand((unsigned)time(NULL)); 13 for(i = 0; i < 10000; i++) 14 { 15 a[i] = rand() % 10; 16 } 17 len = sizeof(a) / sizeof(a[0]); 18 max_appear(a, len); 19 20 system("pause"); 21 return 0; 22 } 23 24 void max_appear(int a[], int len) 25 { 26 int i = 0, j = 0; 27 int num = 0; 28 int *count = NULL; 29 int bop = 0; 30 count = (int*)calloc((num + 2), sizeof(int)); 31 32 for(i = 0; i < len; i++) 33 { 34 for(j = 0; j < i; j++) 35 { 36 bop = 0; 37 if(a[j] == a[i]) 38 { 39 bop = 1; 40 break; 41 } 42 } 43 if(bop == 1) 44 { 45 continue; 46 } 47 48 count[num] = a[i]; 49 50 for(j = i; j < len; j++) 51 { 52 if(a[i] == a[j]) 53 { 54 count[num + 1]++; 55 } 56 } 57 num += 2; 58 count = (int*)realloc(count, (num + 2) * sizeof(int)); 59 count[num + 1] = 0; 60 } 61 62 for(i = 0; i < num; i += 2) 63 { 64 printf("数字%d的个数是%d. ", count[i], count[i + 1]); 65 } 66 67 free(count); 68 count = NULL; 69 }