c++ - How do I find the number that occurs the most in an array?
问题描述
const int NUM_MAX = 100;
const int HOWMANY = 30;
int main()
{
vector<int> myVector;
int counters[NUM_MAX] = { 0 };
//to print out 15 numbers per line
int x = 15;
//seed random number generator
srand(time(0));
cout << "Array of 30 Random Numbers: "<< endl;
for (int i = 0; i < HOWMANY; i++)
{
int myRandom = rand() % NUM_MAX;
myVector.push_back(myRandom);
int thisVal = myVector[i];
cout << thisVal << " ";
if (i % x == 0)
cout << "\n";
}
cout << " " << endl;
return 0;
}
I'm just starting to learn about vectors and arrays, and my teacher has the counters
array set up for us, but I'm not sure what to do with that. I have to find the number that occurs the most in the 30 random generated numbers, and I have to count how many times that number appeared. I'm very lost.
解决方案
我认为你让你自己的问题变得更难,通过让你的问题更容易思考,你可以更快地解决你的问题,正如我的朋友所说,在这个问题中,你可以将一个数组定义为一个计数器并计算其索引中的每个数字,比如在页面顶部说:)
推荐阅读
- c++ - Priority_queue 排序错误,如何解决?
- google-translate - 翻译不带空格的网址
- azure-active-directory - 在 Azure AD 中存储机密数据
- python - python 请求:对天蓝色的 PUT 请求失败并出现 415 错误
- csv - 即使在 12 小时后也没有完成加载 csv 的查询
- python - Alexa 中位置服务的端点是什么?
- sql - SQL Server - 带有变量和 SUM() 方法的 SELECT 语句
- amazon-web-services - Dynamodb 成本与 Aurora 成本比较
- windows - Windows和嵌入式系统(STM32)之间的串行通信数据问题(C/C++)
- postgresql - 与原始数据库相比,还原转储的物化视图上的搜索查询速度较慢