如何在一堆数字中查找众数的代码
时间: 2023-04-02 14:03:08 浏览: 102
寻找众数代码(快速排序)
以下是查找众数的代码:
#include <stdio.h>
int main() {
int n, i, j, maxCount = , mode, count;
scanf("%d", &n);
int a[n];
for (i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = ; i < n; i++) {
count = ;
for (j = ; j < n; j++) {
if (a[j] == a[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = a[i];
}
}
printf("%d", mode);
return ;
}
阅读全文