在C 矢量中找到最常见的数字
Finding the most frequent number(s) in a c++ vector
新家伙和新秀程序员。谁能给我一个想法,我必须完成我的程序任务?
因此,首先,用户必须输入向量的长度,然后输入元素本身(元素只能在0到9之间)。
然后,控制台必须打印出最常见的数字及其发生的次数。但是,如果多个数字具有相同的最大频率,则必须打印所有这些数字,从最小到最大。
到目前为止,一切都很好,我设法为这样的输入做到了:
" 13
4 1 1 4 2 3 4 4 1 2 4 9 3"
的输出显示4是最常见的,发生了5次。
但是:
" 11
7 7 7 0 2 2 2 0 9 9 9"
我可悲地撞到了墙,无法想到一种解决这个问题的简单方法。
预先感谢您决定阅读和伸出援手的人!
blockquote
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int i = 0;
int length;
int elements;
std::cout << "Enter length and elements of array: ";
std::cin >> length;
if (length < 0) { std::cout << "Don't be naughty now!"; return 0; }
std::vector<int> arr;
arr.reserve(length);
for (int i = 0; i<length; i++)
{
std::cin >> elements;
if (elements > 9 || elements < 0)
{
std::cout << "Input can be only between 0 and 9.";
}
else
{
arr.push_back(elements);
}
}
std::sort(arr.begin(), arr.end());
int counter = 0;
int element = 0;
// std::vector<int> mostFreqNums;
for (auto i : arr)
{
int tempElement = i;
int tempCount = 0;
for (auto j : arr)
{
if (j == tempElement)
{
tempCount++;
if (tempCount >= counter)
{
element = tempElement;
counter = tempCount;
}
}
}
}
//for (auto p : mostFreqNums)
//{
std::cout << "The number " << element << " is the most frequent (occurs " << counter << " times).n";
//}
return 0;
}
blockquote
此伪代码可能会有所帮助。
/*a counter for each number 0-9*/
unsigned char counter[10U];
/*keep track of maximum occurrences.*/
unsigned char max_count = 0U;
unsigned char i;
for (i = 0U; i < your_array_len; i++)
{
/*Don't access array out of bounds...*/
if (your_array[i] >= 0U && your_array[i] <= 9U)
{
counter[your_array[i]]++;
if (counter[your_array[i]] > max_count)
{
max_count = counter[your_array[i]];
}
}
}
for (i = 0U; i < 10U; i++)
{
if (counter[i] == max_count)
{
printf("Found %u occurrences for number %un", counter[i], i);
}
}
终于做到了!我会重构并使其更简单,更漂亮,但只是想首先在这里分享。谢谢大家帮助我解决它!
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int length = 0;
int element = 0;
int counter = 0;
int largest = 0;
int i = 0;
int arr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
std::cout << "Enter length and elements (can be between 0 and 9 only): ";
std::cin >> length;
for (i = 0; i < length; i++)
{
std::cin >> element;
if (element < 0 || element > 9)
{
std::cout << "Don't be naughty now!n";
}
else
{
arr[element]++;
}
}
for (i = 0; i < 10; i++)
{
if (arr[i] == arr[i + 1])
{
counter++;
if (counter > largest)
{
largest = counter;
}
}
else
{
counter = 0;
}
}
for (i = 0; i < 10; i++)
{
if (arr[i] == largest)
{
printf("The number(s) %u occur(s) %u timesn", i, arr[i]);
}
}
return 0;
}
您可以使用以下代码:
vector <int> v;
// add some elements
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(1);
int maxCount = 0, mostElement = *(v.begin());
int sz = v.size(); // to avoid calculating the size every time
for(int i=0; i < sz; i++)
{
int c = count(v.begin(), v.end(), v.at(i));
if(c > maxCount)
{ maxCount = c;
mostElement = v.at(i);
}
}
cout << "the number appeared the most is " << mostElement <<"n";
cout << "it appered " <<maxCount<<" times";
注意:如果整数无法保留矢量的大小,则可以将以下循环使用以避免任何迭代器溢出:
for(auto it= v.begin(); it != v.end(); it++)
{
int c = count(v.begin(), v.end(), *it);
if(c > maxCount)
{ maxCount = c;
mostElement = *it;
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 从列表到数组C 查找模式或最常见的数字
- 在C 矢量中找到最常见的数字
- 数组中最常见的数字范围
- 查找整数C++中最不常见的数字
- 不同的编译器用来处理数字转换中的溢出的常见策略是什么?
- 在一大堆数字中找到最常见的六胞胎