查找整数数组中数字的最大出现次数
Finding the largest Occurrence of a digit in an array of integers
我需要一些关于要求编写函数以查找出现次数最大/最小的偶数的赋值的建议。
我的输出应如下所示:
How many integers (to be worked on) ? 2
Enter integer #1: 1230476
Enter integer #2: 10034850
Occurrence of all existing digits --
Digit 0 : 4
Digit 1 : 2
Digit 2 : 1
Digit 3 : 2
Digit 4 : 2
Digit 5 : 1
Digit 6 : 1
Digit 7 : 1
Digit 8 : 1
Digit 9 : 0
Occurence of all existing EVEN digits --
Digit 0 : 4
Digit 2 : 1
Digit 4 : 2
Digit 6 : 1
Digit 8 : 1
出现次数最多的偶数 - 0
和出现次数 : 4
出现次数最少的偶数 - 2 6 8 和出现次数 : 1
这是我到目前为止的代码...我似乎无法找到最大/最小事件的最后一部分。
这是我到目前为止的代码: void displayDigitInfoUpdateStanDeng(( {
int intsWorkedOn;
int* intValue;
int allDigitCount[10] = {0};
int largestOccurEven;
int smallestOccurEven;
int curDigit;
cout << "n Calling on displayDigitInfoUpdateStanDeng() --"
<< "n How many integers (to be worked on) ? ";
cin >> intsWorkedOn;
intValue = new int[intsWorkedOn];
for (int i = 0; i < intsWorkedOn; i++) {
cout << " Enter integer #" << i + 1 << ": ";
cin >> *(intValue + i);
}
for (int i = 0; i < intsWorkedOn; i++) {
do {
allDigitCount[*(intValue + i) % 10]++;
*(intValue + i) /= 10;
} while (*(intValue + i));
}
cout << "n Occurence of all existing digits --";
for (int i = 0; i < 10; i++) {
cout << "n Digit " << i << " : " << allDigitCount[i];
}
cout << "n Occurence of all existing EVEN digits --";
for (int i = 0; i < 9; i++) {
cout << "n Digit " << i - 1 << " : " << allDigitCount[i++];
}
cout << "n The even digit(s) that has/have the largest occurrence -";
for (int i = 0; i < 9; i++) {
largestOccurEven = allDigitCount[i++] % 10;
curDigit = allDigitCount[i++];
if (curDigit < largestOccurEven) {
cout << "n " << i
<< "n And the number of occurrence(s) : " << largestOccurEven;
} else {
cout << endl;
}
}
这是我当前的输出:
有多少个整数(要处理(? 2 输入整数 #1:1230476 输入整数 #2:10034850
Occurrence of all existing digits --
Digit 0 : 4
Digit 1 : 2
Digit 2 : 1
Digit 3 : 2
Digit 4 : 2
Digit 5 : 1
Digit 6 : 1
Digit 7 : 1
Digit 8 : 1
Digit 9 : 0
Occurence of all existing EVEN digits --
Digit 0 : 4
Digit 2 : 1
Digit 4 : 2
Digit 6 : 1
Digit 8 : 1
出现次数最多的偶数 - 2
和出现次数 : 4
出现次数最少的偶数 - ? 和出现次数 : 0
我好困惑...为什么它将 i 显示为 2 表示出现次数最多?我真的需要帮助!
在 for 循环中执行这样的i++
意味着您在以下每个步骤中都查看不同的"箱":
largestOccurEven = allDigitCount[i++] % 10;
curDigit = allDigitCount[i++];
你会想避免这样做。 例如,将两者更改为简单i
并适当更改for
循环:
for (int i = 0; i < 9; i += 2) {
相关文章:
- 从二进制文件中读取整数数组
- 新的整数数组向右移动?
- C++ 中的二维整数数组,每行中的元素数量不均匀
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 在 C++ 中将整数数组转换为位集表示形式的最佳方法?
- 从两个 4x64 位整数数组中获取取模
- 字符到整数数组
- 从函数参数 [C++] 复制整数数组
- 指向整数数组和打印总和的指针
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 线程安全整数数组?
- 整数数组中最长的回文
- 在大整数数组中查找子数组
- 查找整数数组中最接近的数字
- 创建<int>对整数数组指针的矢量引用 (C++)
- 整数指针数组的元素是否可以指向整数数组?
- 如何将整数数组相乘得到一个数字?
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 将文本文件读入整数数组