1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-12-16 15:44:39 +00:00
202401-programming-assignments/【实践课外】7.数组1/7-6 求一批整数中出现最多的个位数字.c

55 lines
872 B
C

#include <stdio.h>
int n, cnt[15];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
if (x == 0) {
cnt[0]++;
} else {
while (x) {
cnt[x % 10]++;
x /= 10;
}
}
}
int max = 0;
for (int i = 0; i <= 9; i++) {
if (cnt[i] > max) {
max = cnt[i];
}
}
int max_cnt = 0, out_cnt = 0;
for (int i = 0; i <= 9; i++) {
if (cnt[i] == max) {
max_cnt++;
}
}
printf("%d: ", max);
for (int i = 0; i <= 9; i++) {
if (cnt[i] == max) {
out_cnt++;
if (out_cnt < max_cnt) {
printf("%d ", i);
} else {
printf("%d\n", i);
}
}
}
return 0;
}