我有一个项目在c + +中接收数组,我需要建立一个程序,将只打印出现超过3倍+他们的索引的数字。 例如,对于阵列6,4,4,5,2,4,4,3,5,5 - 它将打印: 4:1,2,5,6 5:3,8,9排序数组 - 不断收到错误。
和最重要的 - 它不应该超过O(n * log n)。
的sooo ....对这个问题...
这是我不断收到错误:
1>HW - 2.obj : error LNK2019: unresolved external symbol "void __cdecl mergeSortP(int * const,int)" ([email protected]@[email protected]) referenced in function "void __cdecl checkIfNumberIsMoreThenThreeTimes(int * const,int)" ([email protected]@[email protected])
1>C:\documents\visual studio 2012\Projects\HW - 2\Debug\HW - 2.exe : fatal error LNK1120: 1 unresolved externals
,这是代码:
void checkIfNumberIsMoreThenThreeTimes(int arr[], int n)
{
int **p;
p = copyToPointersArr(arr, n);
mergeSortP(*p, n);
output(arr, p, n);
}
//
int** copyToPointersArr(int *arr, int n)
{
int **pointers;
for (int i=0; i<n; i++)
*pointers[i]=arr[i];
return pointers;
}
//
void merge(int **a1, int **a2, int size1, int size2, int **res)
{
int ind1, ind2, ind;
ind1=ind2=ind=0;
while (ind1<size1 && ind2<size2)
{
if (*a1[ind1]<=*a2[ind2])
{
res[ind]=a1[ind1];
ind1++;
}
else
{
res[ind]=a2[ind2];
ind2++;
}
ind++;
}
while (ind1<size1)
{
res[ind]=a1[ind1];
ind1++;
ind++;
}
while (ind2<size2)
{
res[ind]=a2[ind2];
ind2++;
ind++;
}
}
//
void mergeSortP(int **a, int size)
{
int i;
int **temp=NULL;
if (size==1)
return;
else
{
mergeSortP(a, size/2);
mergeSortP(a+(size/2), size-(size/2));
temp = new int* [size];
merge(a, a+(size/2), size/2 , size-(size/2), temp);
for (i = 0; i < size; i++)
a[i] = temp[i];
delete []temp;
temp=NULL;
}
}
//
void output(int arr[], int **ptr,int size)
{
int i, j, count=0;
for (i = 0; i < size-1; i++)
{
if(*ptr[i]==*ptr[i+1])
count++;
else if (count>=2)
{
cout << *ptr[i] << ": ";
for (j = count; j >= 0; j--)
cout << (ptr[i-j]-arr) << ", ";
count=0;
}
else
count=0;
}
}
请帮助! 在此先感谢....
mergeSortP想要一个双指针。您正试图传递单个指针。 –
@ user3328918 - 请原谅我的无知,但是如何使用合并排序解决您的问题,即计算数组中出现的数字的次数并列出索引?如果你真的在学习C++,那么显而易见的解决方案是使用std :: map,其中SomeStruct是一个包含count和一个包含找到的索引的向量的结构。大概5或6行,最多写10个。 –
PaulMcKenzie
我同意@PaulMcKenzie。这些日子真的是C++教育的传承吗? _双指针?学习C++的人不会对_regular_指针有足够的麻烦吗? – Chad