查找c ++向量中最常见的数字

时间:2021-09-12 13:13:06

New guy here, and a rookie programmer. Can anyone give me an idea how to finish up my program task I gotta do?

这里有新人,还有菜鸟程序员。任何人都可以告诉我如何完成我必须完成的程序任务?

So first the user has to input the length of the vector, afterwards input the elements themselves (elements can be only between 0 and 9).

因此,首先用户必须输入向量的长度,然后输入元素本身(元素只能在0到9之间)。

Then the console has to print out the most frequent number and the number of times it occurs. But in case of multiple numbers with the same maximal frequency, it has to print all of them, ordered from smallest to largest.

然后控制台必须打印出最频繁的号码及其发生的次数。但是如果多个数字具有相同的最大频率,则必须打印所有这些数字,从最小到最大排序。

So far, so good, I've managed to do it for an input like this:

到目前为止,这么好,我已经设法做了这样的输入:

"13

4 1 1 4 2 3 4 4 1 2 4 9 3"

4 1 1 4 2 3 4 4 1 2 4 9 3“

And have an output that says that 4 is the most frequent, occurring 5 times.

并且有一个输出表明4是最频繁的,发生5次。

But for:

"11

7 7 7 0 2 2 2 0 9 9 9"

7 7 7 0 2 2 2 0 9 9 9“

I sadly hit a wall and cannot think of a simple way to tackle this.

我不幸碰到了一堵墙,想不出一个简单的方法来解决这个问题。

Thank you in advance to whoever decides to read and lend a hand!

提前感谢您决定阅读并伸出援手的人!

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

2 个解决方案

#1


0  

This pseudo code might help.

这个伪代码可能有所帮助。

/*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 %u\n", counter[i], i);
    }
}

#2


0  

Finally did it! I will refactor and make it simpler and prettier, but just wanted to share it here first. Thank you everyone for helping me solve it!

终于做到了!我将重构并使其更简单和漂亮,但只是想先在这里分享。谢谢大家帮我解决!

#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 times\n", i, arr[i]);
        }
    }

    return 0;
}

#1


0  

This pseudo code might help.

这个伪代码可能有所帮助。

/*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 %u\n", counter[i], i);
    }
}

#2


0  

Finally did it! I will refactor and make it simpler and prettier, but just wanted to share it here first. Thank you everyone for helping me solve it!

终于做到了!我将重构并使其更简单和漂亮,但只是想先在这里分享。谢谢大家帮我解决!

#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 times\n", i, arr[i]);
        }
    }

    return 0;
}