查找整数数组中数字的最大出现次数

Finding the largest Occurrence of a digit in an array of integers

本文关键字:整数 数组 数字 查找      更新时间:2023-10-16

我需要一些关于要求编写函数以查找出现次数最大/最小的偶数的赋值的建议。

我的输出应如下所示:

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) {