求一批整数中出现最多的个位数字
时间: 2024-05-13 07:13:32 浏览: 58
假设我们有一批整数,存储在一个数组中,那么我们可以使用一个计数器数组来记录每个数字出现的次数。具体步骤如下:
1. 定义一个长度为10的计数器数组count,用来记录0~9每个数字出现的次数,初始值都为0。
2. 遍历整数数组,对于每个整数,取它的个位数字,然后在count数组中对应的位置加1。
3. 遍历count数组,找出出现次数最多的数字,即count数组中最大值所对应的下标,该下标就是出现最多的个位数字。
下面是一个Python实现的例子:
```
def most_frequent_digit(nums):
count = [0] * 10
for num in nums:
digit = num % 10
count[digit] += 1
max_count = max(count)
return count.index(max_count)
# 示例用法
nums = [123, 456, 789, 321, 654, 987, 111, 222, 333]
print(most_frequent_digit(nums)) # 输出1,因为数字1出现了3次,是出现最多的个位数字。
```
注意,上面的代码只考虑了个位数字,如果要找出出现最多的十位、百位等等,可以将计数器数组改为一个多维数组,每个维度对应一个数字位。
相关问题
求一批整数中出现最多的个位数字c++
### 回答1:
你好,以下是求一批整数中出现最多的个位数字的C++代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums; // 存储整数的容器
int n; // 整数个数
cin >> n;
for (int i = ; i < n; i++) {
int num;
cin >> num;
nums.push_back(num);
}
vector<int> counts(10, ); // 存储每个个位数字出现次数的容器
for (int i = ; i < n; i++) {
int num = nums[i];
while (num != ) {
int digit = num % 10;
counts[digit]++;
num /= 10;
}
}
int max_count = *max_element(counts.begin(), counts.end()); // 最多出现次数
int max_digit; // 最多出现的个位数字
for (int i = ; i < 10; i++) {
if (counts[i] == max_count) {
max_digit = i;
break;
}
}
cout << max_digit << endl;
return ;
}
```
该代码首先读入整数个数和整数,然后遍历每个整数,统计每个个位数字出现的次数,最后找到出现次数最多的个位数字并输出。
### 回答2:
首先需要遍历整个整数数组,统计每个数字在个位上出现的次数。这可以通过循环数组,对每个数取模10来实现。
例如,对于整数123,取模10的结果为3,即3是个位数字。再对下一个整数456取模10,结果为6,以此类推。
通过这种方式,可以得到每个数字在个位上出现的次数,将其保存在一个计数数组中。假设计数数组为countArray,其下标为数字0~9,countArray[i]表示数字i在个位上出现的次数。
接下来,需要找出出现次数最多的数字,即出现最多的个位数字c。可以通过遍历countArray数组,找到出现次数最多的countArray[c]。如果存在多个出现次数相同的数字,则选择最大的数字。
代码实现如下:
int[] array = {123, 456, 789, 321, 654, 987};
int[] countArray = new int[10];
for (int i = 0; i < array.length; i++) {
int num = array[i] % 10;
countArray[num]++;
}
int maxCount = 0;
int digit = 0;
for (int i = 0; i < countArray.length; i++) {
if (countArray[i] > maxCount) {
maxCount = countArray[i];
digit = i;
} else if (countArray[i] == maxCount && i > digit) {
digit = i;
}
}
System.out.println("出现最多的个位数字是:" + digit);
综上所述,通过遍历整数数组并取模10,可以统计每个数字在个位上出现的次数。通过遍历计数数组,可以找到出现次数最多的数字。这样就可以求出一批整数中出现最多的个位数字c。
### 回答3:
假设我们有一批整数 $a_1, a_2, \dots, a_n$,我们要求其中出现最多的个位数字 $c$。
我们可以使用一个计数器数组 $count$,长度为 $10$,对于每个整数,我们将它的个位数字取出来,然后将对应的计数器加 $1$。最后,我们遍历整个计数器数组,找到计数器最大的位置 $i$,即为出现最多的个位数字 $c$。
这个算法的时间复杂度为 $O(n)$,因为我们只需要对每个整数进行一次操作,并且需要遍历计数器数组一次。
下面是具体的实现代码:
```python
def most_frequent_digit(nums):
count = [0] * 10 # 初始化计数器数组
for num in nums:
digit = num % 10 # 取出个位数
count[digit] += 1 # 对应计数器加 1
max_count = -1
most_frequent_digit = -1
for i in range(10):
if count[i] > max_count:
max_count = count[i]
most_frequent_digit = i
return most_frequent_digit
```
其中,参数 `nums` 是一个整数数组,返回值为出现最多的个位数字。
用c语言求一批整数中出现最多的个位数字
以下是用C语言求一批整数中出现最多的个位数字的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, num, max_count = 0, max_num = 0;
int count[10] = {0}; // 记录每个数字出现的次数
scanf("%d", &n); // 输入整数的个数
for (int i = 0; i < n; i++)
{
scanf("%d", &num); // 输入整数
while (num > 0)
{
int digit = num % 10; // 取出个位数字
count[digit]++; // 对应数字的计数器加1
num /= 10; // 去掉个位数字
}
}
for (int i = 0; i < 10; i++)
{
if (count[i] > max_count) // 找出出现次数最多的数字
{
max_count = count[i]; max_num = i;
}
}
printf("出现次数最多的个位数字是:%d,出现了%d次。\n", max_num, max_count);
return 0;
}
```