给定一批整数,分析每个整数的每一位数字,求出现次数最多的个位数字。例如给定3个整数1234、2345、3456,其中出现最多次数的数字是3和4,均出现了3次。
输入格式:
输入在第1行中给出正整数N(≤1000),在第二行中给出N个不超过整型范围的非负整数,数字间以空格分隔。
输出格式:
在一行中按格式“M: n1 n2 ...”输出,其中M是最大次数,n1、n2、……为出现次数最多的个位数字,按从小到大的顺序排列。数字间以空格分隔,但末尾不得有多余空格。
输入样例:
3
1234 2345 3456
输出样例:
3: 3 4
#include <stdio.h>
int main(){
// gewei:用于取个位数 shu[10]:设置一个数组用于存放个位数出现的个数
int N, i, gewei, shu[10] = {0};
scanf("%d", &N);
int nums[N];
for( i=0; i<N; i++ ){
scanf(" %d", &nums[i]);
int x = nums[i];
while( x != 0 ){
gewei = x % 10;
switch( gewei ){
case 0:
shu[0]++;
break;
case 1:
shu[1]++;
break;
case 2:
shu[2]++;
break;
case 3:
shu[3]++;
break;
case 4:
shu[4]++;
break;
case 5:
shu[5]++;
break;
case 6:
shu[6]++;
break;
case 7:
shu[7]++;
break;
case 8:
shu[8]++;
break;
case 9:
shu[9]++;
}
x /= 10;
}
}
int max = shu[0];
for( i=1; i<10; i++ ) // 取最大次数
if( max < shu[i] )
max = shu[i];
printf("%d:", max);
for( i=0; i<10; i++ )
if( max == shu[i] )
printf(" %d", i);
return 0;
}