【发布时间】:2014-01-22 05:20:13
【问题描述】:
如果有多个数字重复相同的次数,我无法弄清楚如何打印“无模式”。 5 5 6 6 7 6 9;因为 5 和 6 都重复了两次,所以我想打印出“无模式”这是我用来查找模式的算法:
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;
}
【问题讨论】:
-
虽然通过地图可以很容易地计算出出现的频率,但是对于这个特定的代码,如果你解释一下你的算法会有所帮助。