如果有多於一個數字重複相同的次數,我不知道如何打印「無模式」恩。 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;
}
雖然發生頻率可通過地圖很容易地計算,對於第是特定的代碼,它會幫助你解釋你的算法。 –