给定一批整数,分析每个整数的每一位数字,求出现次数最多数字(输出格式:“M: n1 n2 ...”行末输出不能有空格且“:”后没空格)

#include 
using namespace std;

int main(int argc, char *argv[])
{
	
	int n;
	cin >> n;
	int *a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	int b[10] = { 0 };
	int tmp;
	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			b[0]++;
			continue;
		}
		while (a[i] > 0)
		{
			tmp = a[i] % 10;
			b[tmp]++;
			a[i] /= 10;
		}
	}
	int max = b[0];
	for (int i = 0; i < 10; i++)
	{
		if (b[i] > max) max = b[i];
	}
	cout << max << ":";
	int flag = 0;
	for (int i = 0; i < 10; i++)
	{
		if (b[i] == max)
		{
			if (flag == 1) cout << " ";
			cout << i;
			flag = 1;
		}
	}




	delete[]a;


	return 0;
}

你可能感兴趣的:(练习)