Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何找到排序数组的模式?_C++_Arrays_Algorithm_Sorting - Fatal编程技术网

C++ 如何找到排序数组的模式?

C++ 如何找到排序数组的模式?,c++,arrays,algorithm,sorting,C++,Arrays,Algorithm,Sorting,我需要编写一个函数来查找数组的模式。然而,我不擅长提出算法,我希望其他人知道如何做到这一点 我知道数组的大小和每个元素中的值,并将数组从最小到最大排序 数组将传递给mode函数,如下所示 模式=findMode(arrayPointer,sizePointer) 更新: 在阅读了评论之后,我尝试了这个 int findMode(int *arrPTR, const int *sizePTR) { int most_found_element = arrPTR[0]; int mo

我需要编写一个函数来查找数组的模式。然而,我不擅长提出算法,我希望其他人知道如何做到这一点

我知道数组的大小和每个元素中的值,并将数组从最小到最大排序

数组将传递给mode函数,如下所示

模式=findMode(arrayPointer,sizePointer)

更新:

在阅读了评论之后,我尝试了这个

int findMode(int *arrPTR, const int *sizePTR)
{
    int most_found_element = arrPTR[0];
    int most_found_element_count = 0;
    int current_element = arrPTR[0];
    int current_element_count = 0;
    int count;
    for (count = 0; count < *sizePTR; count++)
    {
        if(count == arrPTR[count])
             current_element_count++;
        else if(current_element_count > most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element = count;
        current_element_count=1;
    }

    return most_found_element;
}
int findMode(int*arrPTR,const int*sizePTR)
{
int most_found_元素=arrPTR[0];
int most\u found\u element\u count=0;
int current_element=arrPTR[0];
int当前元素计数=0;
整数计数;
对于(计数=0;计数<*sizePTR;计数++)
{
if(count==arrPTR[count])
当前元素计数++;
else if(当前元素计数>找到的最多元素)
{
most_found_element=当前_element;
most_found_element_count=当前_element_count;
}
当前元素=计数;
当前元素计数=1;
}
返回找到的最多元素;
}
不过,如果有人能帮我解决的话,我在掌握这个算法方面仍然有困难。
我从来没有使用过向量,所以我不太理解其他的例子。

你几乎拥有了一切

您可以利用数组已排序这一事实

只需遍历数组,跟踪当前相等的连续数,以及在该点之前找到的最大相等的连续数(以及产生它的数字)。最后,您将拥有最大数量的相等连续数字,以及产生该数字的数字。这就是模式

注意:有关不需要对数组进行排序的解决方案,请参见a中的示例。

提示:

问:您如何定义模式

A:数组中计数最大的数字

问:如何计算有序数组中的数字

答:遍历数组,当下一项等于上一项时,增加该值的计数

Q:如果前一个值的计数小于当前值的计数,那么前一个值可以是模式吗

A:没有

我以为名字可以解释,但现在我们开始:

When we start, no element has been found the most times
  so the "high-score" count is zero.
Also, the "current" value is the first, but we haven't looked at it yet 
  so we've seen it zero times so far
Then we go through each element one by one
  if it's the same as "current" value, 
     then add this to the number of times we've seen the current value.
  if we've reached the next value, we've counted all of the "current" value.
     if there was more of the current value than the "high-score"
        then the "high-score" is now the current value
     and since we reached a new value
        the new current value is the value we just reached
Now that we've seen all of the elements, we have to check the last one
  if there was more of the current value than the "high-score"
    then the "high-score" is now the current value
Now the "high-score" holds the one that was in the array the most times!

另请注意:我的原始算法/代码有一个bug,我们必须在循环结束后对“current”进行额外检查,因为它永远不会找到“最后一个之后的那个”。

如果输入数组被排序,这里的方法与其他答案中描述的方法相同,但以不同的方式实现,这是一种更好、更容易理解的方法

  • 在输入数组上运行一个循环
  • 保持全局模式值和模式计数
  • 运行滑动窗口,直到找到相等的元素
  • 如果本地相等元素计数大于全局模式计数,则更新全局模式和模式计数
  • 下面是在
    C++
    中运行和测试的代码

    int mode(vector<int> a, int N)
    {
        int mode = a[0];
        int mode_count = 1;
        int i = 0;
        while (i < N - 1) {
            int cur = a[i];
            int cur_count = 1;
            while (a[i] == a[i + 1]) {
                i++;
                cur_count++;
            }
            if (cur_count > mode_count) {
                mode_count = cur_count;
                mode = a[i];
            }
            i++;
        }
        return mode;
    }
    
    int模式(向量a,int N)
    {
    int mode=a[0];
    int mode_count=1;
    int i=0;
    而(i模式计数){
    模式计数=当前计数;
    模式=a[i];
    }
    i++;
    }
    返回模式;
    }
    
    您能描述一下您尝试过的一些事情并发布一些代码吗?这里给你一个提示:你可以尝试在数组中循环,每次看到一个元素时,将一个特定于元素的计数器增加一个。你想要数组的模式吗?你是说?还是中位数?还是我需要学一个新学期?编辑:@wilhelmtell-数组中重复最多的元素是一个相关问题,请参见。直方图答案()值得注意;即使数组没有排序,它也能工作。我尝试并发布了问题中的代码,但仍然无法理解这一点。你能告诉我我做错了什么吗?@sircrisp:如果最高值是mode,那么我的算法中有一个bug。此外,我还添加了一个解释。我不建议按值传递向量。
    int mode(vector<int> a, int N)
    {
        int mode = a[0];
        int mode_count = 1;
        int i = 0;
        while (i < N - 1) {
            int cur = a[i];
            int cur_count = 1;
            while (a[i] == a[i + 1]) {
                i++;
                cur_count++;
            }
            if (cur_count > mode_count) {
                mode_count = cur_count;
                mode = a[i];
            }
            i++;
        }
        return mode;
    }