我需要输出"no mode"如果多个数字重复相同的次数

i need to output "no mode" if more than one number is repeated the same amount of times

本文关键字:数字 输出 no mode 如果      更新时间:2023-10-16

我无法弄清楚如何打印"否模式",如果有一个以上的数字与ex重复相同的次数。5 5 6 6 7 6 9;由于5和6都重复两次,所以我想打印出"无模式",这是算法im用于查找模式的算法:

int mostfound = *pScores;
int most_found_count = 0;
int currentnum = *pScores;
int current_num_count = 0;
bool noMode = true;

//finding the mode
for (int i =  0; i < numScores; i++)
{
  if (*(pScores + i) == currentnum) 
  {
     current_num_count++;
  }
  else {
      if (current_num_count > most_found_count) 
        {
              mostfound = currentnum; 
              most_found_count = current_num_count;
              noMode = false;
        }
  else if (current_num_count == most_found_count)
        {
            noMode = true;
        }
       currentnum = *(pScores + i); 
       current_num_count = 1;
  }
}
cout << mostfound << endl;
        cout << currentnum << endl;
        cout << most_found_count << endl;
cout << "Mode: " << mostfound << endl;

}

std :: multiset可以帮助您

#include <set>
using namespace std;
....
multiset<int> setScores;
for (int i =  0; i < numScores; i++)
{
    setScores.insert(pScores[i]);
}
// setScores here got items = (a number being repeated)
// and count = (how many times number repeated)
multiset<int> setRepeats;
for (multiset<int>::iterator it = setScores.begin(); it!=setScores.end(); it++)
{
    setRepeats.insert(setScores.count(*it));
}
// setRepeats here got items = (how many times a number repeated) 
// and count = (how many different numbers repeated this amount of times)
// Now search for count that > 1
noMode = false;
for (multiset<int>::iterator it1 = setRepeats.begin(); it1!=setRepeats.end(); it1++)
{
    if(setRepeats.count(*it1)>1)
    {
        noMode = true;
        break;
    }
}
// Now use noMode as you wish

ps注意您的样本数组中的数字6重复3次,但数字5仅重复两次,因此Nomode将为false