本文共 1104 字,大约阅读时间需要 3 分钟。
为了解决给定一批整数,分析每个整数的每一位数字,找出出现次数最多的个位数字的问题,我们可以按照以下步骤进行:
#includeint main() { int n, j; int num[10] = {0}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &j); while (j != 0) { num[j % 10]++; j /= 10; } } int current_max = 0; for (int i = 1; i < 10; i++) { if (num[i] > current_max) { current_max = num[i]; } } printf("%d: ", current_max); int count = 0; for (int i = 0; i < 10; i++) { if (num[i] == current_max) { if (count) { printf(" "); } printf("%d", i); count++; } } printf("\n"); return 0;}
scanf
函数读取输入的正整数N和后续的N个整数。num
来记录每个数字(0-9)的出现次数。对于每个整数,使用while
循环逐个提取每一位数字,并更新相应的计数器。num
,找出出现次数最多的数字。通过这种方法,我们可以准确地分析每个整数的每一位数字,找出出现次数最多的个位数字,并以指定的格式输出结果。
转载地址:http://jsgfk.baihongyu.com/