我试图找到恒定大小数组的中值。但阵列总是在上升。我的意思是用旧数字代替新数字。我称这个过程运行中位数,或者我们可以说飞行中位数。这是我的代码和代码内部,当rand()函数生成78时,代码找不到正确的中位数。 (在生成78; 41,67,34,0,69,24之前)
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#define MAX_SIZE 5
using namespace std;
bool isOdd( int integer )
{
if ( integer % 2 == 0 )
return false;
else
return true;
}
int main()
{
int median;
int *minArray ;
int *maxArray ;
int myArray[MAX_SIZE];
for(int i=0; i<20; i++)
{
int v = rand() %100;
cout << v << endl;
myArray[i%MAX_SIZE] = v;
if(i==0)
{
median = v;
}
else if (v>median)
{
maxArray= new int [MAX_SIZE+1];
int n;
for(n=0; n<(MAX_SIZE+1); n++)
{
if ((median<myArray[n])&&(myArray[n]<=v))
{
maxArray[n] = myArray[n];
//cout<<"asda"<<maxArray[n]<<endl;
}
else
{
maxArray[n] = 200;
//cout<<"asda"<<maxArray[n]<<endl;
}
}
if(isOdd(i)&&(i<MAX_SIZE))
median = (median+(*min_element(maxArray,maxArray+MAX_SIZE+1)))/2;
else median = (*min_element(maxArray,maxArray+MAX_SIZE+1));
//cout << ((*min_element(maxArray,maxArray+MAX_SIZE+1))) << endl;
delete [] maxArray;
}
else if (v<median)
{
minArray= new int [MAX_SIZE+1];
int n;
for(n=0; n<(MAX_SIZE+1); n++)
{
if ((median>myArray[n])&&(myArray[n]>=v))
{
minArray[n] = myArray[n];
//cout<<"asda"<<minArray[n]<<endl;
}
else
{
minArray[n] = 0;
//cout<<"asda"<<minArray[n]<<endl;
}
}
if(isOdd(i)&&(i<MAX_SIZE))
median = (median+(*max_element(minArray,minArray+MAX_SIZE+1)))/2;
else median = (*max_element(minArray,minArray+MAX_SIZE+1));
delete [] minArray;
}
cout << "median: "<< median<<endl;
}
return 0;
}
如果我在解释问题上犯了一些错误,请原谅我这里是新人。
答案 0 :(得分:1)
我认为可能有另一种方式。
由于您现在是数据集的边界,如:int v = rand() %100;
所示,您还可以跟踪每个数字的出现次数。
您需要将出现次数存储在长度为100的数组中。您还需要跟踪出现的数字&#39; out&#39;减少发生次数。
如果你有这个,只要你的出现次数大于MAX_SIZE / 2就可以从0 ... 100循环。
这将是一个0(n)操作但具有显着的开销,特别是因为数字0 ... 100的范围比MAX_SIZE更大(反过来会更好)。
总之;我想如果你应用这个原则,你也不会对更换数组有问题。
如果您愿意,我可以为您提供一个快速示例。
修改强>
此示例效果不佳,但您可以尝试一下:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#define MAX_ELEMENTS 5
#define MAX_VALUE 100
using namespace std;
bool isOdd( int integer )
{
if ( integer % 2 == 0 )
return false;
else
return true;
}
int main()
{
int median;
int numberOfElements = 0;
int myValueArray[MAX_VALUE];
int myArray[MAX_ELEMENTS];
//quick n dirty init
for (int c = 0; c < MAX_VALUE; c++)
myValueArray[c] = 0;
for (int c = 0; c < MAX_ELEMENTS; c++)
myArray[c] = 0;
for(int i=0; i<20; i++)
{
//generate random number 0...100
int v = rand() % MAX_VALUE;
cout << "| " << v << " | "; //incomming value
myValueArray[v]++;
int leavingValue = myArray[i%MAX_ELEMENTS];
myArray[i%MAX_ELEMENTS] = v; // just to keep track of leaving value
if (numberOfElements < MAX_ELEMENTS)
numberOfElements++;
else //remove leaving value
{
myValueArray[leavingValue]--;
cout << "| " << leavingValue << " | "; //leaving value
}
for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
{
occurances += myValueArray[c];
//(numberOfElements + 1) = dirty indexer correction, but you'll get the point
if (occurances >= (numberOfElements + 1) / 2)
{
if (isOdd(numberOfElements))
median = c;
else
cout << "work to do here...";
break;
}
}
cout << "array: ";
//just print the array, to confirm
for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
{
if (myValueArray[c] > 0)
{
for (int x = 0; x < myValueArray[c]; x++)
cout << " {" << c << "}, ";
}
}
cout << " >> median: "<< median<<endl;
}
return 0;
}