code4sk/c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/求一批整数中出现最多的个位数字.cpp
2023-12-15 22:38:26 +08:00

59 lines
No EOL
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
7-5 求一批整数中出现最多的个位数字
分数 10
作者 徐镜春
单位 浙江大学
给定一批整数分析每个整数的每一位数字求出现次数最多的个位数字。例如给定3个整数1234、2345、3456其中出现最多次数的数字是3和4均出现了3次。
输入格式:
输入在第1行中给出正整数N≤1000在第二行中给出N个不超过整型范围的非负整数数字间以空格分隔。
输出格式:
在一行中按格式“M: n1 n2 ...”输出其中M是最大次数n1、n2、……为出现次数最多的个位数字按从小到大的顺序排列。数字间以空格分隔但末尾不得有多余空格。
输入样例:
3
1234 2345 3456
输出样例:
3: 3 4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
*/
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[n], b[10], c;
for (int i = 0; i < 10; i++)
b[i] = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i] == 0)
b[0]++;
for (; a[i] != 0; a[i] /= 10)
{
c = a[i] % 10;
b[c]++;
}
}
int max = b[0];
for (int i = 1; i < 10; i++)
{
if (max < b[i])
max = b[i];
}
printf("%d:", max);
for (int i = 0; i < 10; i++)
{
if (max == b[i])
printf(" %d", i);
}
return 0;
}