回复 10楼 小小战士
那要怎么写
#include<stdio.h> struct Count { int c; int count; }; int main() { int list[2][3]={4,55,6,44,55,55}; struct Count conter[10000]; int i,j; for(i = 0; i < 10000; i++) { conter[i].c = i; conter[i].count = 0; } for(i =0; i < 2 ; i ++) for(j=0;j<3;j++) conter[list[i][j]].count ++; int max=0; for(i = 0; i < 10000; i++) { if( conter[i].count>0 ) { if (conter[i].count>max) {max=conter[i].count;j=i;}; } } printf("出现次数最多的数是=%d\n",j); return 0; }我倒是实现了一个,不过感觉写的很乱 看看其他人的代码吧
#include <stdio.h> #include <string.h> #define M 3 #define N 3 #define Mount (M * N) struct Flag { int key; int count; }flag[Mount]; void InitFlag(int a[M][N]) { int i, j, temp; for (i = 0;i < M;i++) { for (j = 0;j < N;j++) { temp = a[i][j] % Mount; if (!flag[temp].key || a[i][j] == flag[temp].key) { flag[temp].key = a[i][j]; flag[temp].count += 1; continue; } while (flag[temp].key && a[i][j] != flag[temp].key) temp = (temp + 1) % Mount; flag[temp].key = a[i][j]; flag[temp].count += 1; } } } void OutPut() { int i, j = 0, max = 0; for (i = 0;i < Mount;i++) { if (max < flag[i].count) { max = flag[i].count;j = i; } } printf("出现次数最多的是:%d\n", flag[j].key); } int main() { int a[M][N] = {1, 14, 3, 14, 14, 5, 16, 5, 7}; memset(flag, 0, sizeof(flag)); InitFlag(a); OutPut(); return 0; }