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

标签 c++ arrays find-occurrences

我需要一些关于作业的建议,该作业要求编写一个函数来查找出现次数最多/最少的偶数。

我的输出应该是这样的:

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++ 意味着您在每个步骤中都在不同的“bins”中查找:

largestOccurEven = allDigitCount[i++] % 10;

curDigit = allDigitCount[i++];

你会想避免这样做。例如,将两者更改为简单的 i 并适当更改 for 循环:

for (int i = 0; i < 9; i += 2) {

关于c++ - 查找整数数组中数字的最大出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19236986/

相关文章:

c++ - 在基类中调用 shared_from_this() 时的 bad_weak_ptr

使用流和缓冲区的 C++ 最佳实践

Php 数组排序服装尺寸(XXS XS S M L XL XXL)和动态数组上的数字

java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数

c++ - 将 C++ 类放在另一个文件中 : compilation error

c++ - 字符串的第 n 个字符到 int

elasticsearch - ElasticSearch Analyzer:有没有办法删除索引中出现的相同单词?

java - 使用Java快速计算字符串中单词出现次数的方法

Java - 在数组干草堆中查找针的一部分的索引

javascript - 当对象可以具有不同类型时如何按键对对象数组进行分组