我需要输出"no mode"如果多个数字重复相同的次数
i need to output "no mode" if more than one number is repeated the same amount of times
我无法弄清楚如何打印"否模式",如果有一个以上的数字与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
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何将输出数字划分为奇数和偶数
- 输出错误,问题是找到总和5000位数字cpp
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 以C++输出一个数字三角形
- C++输出奇怪的字符而不是数字 (Windows)
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- 反向输出数字 - c++
- 为什么当我输入一个大数字时,输出会一遍又一遍地重复?
- 未来值公式显示疯狂巨大数字的输出
- 输出一个数字,该数字可能是三种类型之一
- 预期输出之后的所有这些数字是多少
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 素数检查 C++ 函数输出非素数的数字
- 如何将数字输出到文件中,所有数字都具有相同的精度
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- C++ 如何从输入字母或数字输出字母或数字
- gcc 预处理的表单哈希符号 + 数字"# 1"输出行是什么意思?
- C++ 意外的数字输出
- 数据将奇怪的数字输出到文件,MAP C++