在C 矢量中找到最常见的数字

Finding the most frequent number(s) in a c++ vector

本文关键字:常见 数字      更新时间:2023-10-16

新家伙和新秀程序员。谁能给我一个想法,我必须完成我的程序任务?

因此,首先,用户必须输入向量的长度,然后输入元素本身(元素只能在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;
        }
    }