这实际上取决于您可以使用多少 C++ 的优点。鉴于您现有的代码,所使用的 C++ 没有什么独特之处。这可能是你的导师在你课堂的这个阶段想要的。如果是这种情况,那么一个简单的手动排序例程,然后调用一个传递数据的函数,数据的大小,一个相同大小的数组来保存与请求的频率匹配的值,并且要查找的频率是基本的你可以得到。例如
#include <iostream>
using namespace std;
int countfreq (int *data, int dsize, int *freqvals, int freq)
{
int last = *data, /* set last to first value in data */
idx = 0, /* index for freqvals to return */
seq = 1; /* sequential values count */
if (freq <= 0 || freq > dsize) /* is requested freq valid? */
return 0;
for (int i = 1; i < dsize; i++) { /* loop 2nd value to end */
if (last == data[i]) /* if last == current */
seq++; /* increment sequential count */
else { /* otherwise */
if (seq == freq) /* sequential count == freq? */
freqvals[idx++] = last; /* add last value to freqvals */
seq = 1; /* reset sequential count 1 */
}
last = data[i]; /* set last = current */
}
if (seq == freq) /* handle last value in data */
freqvals[idx++] = last;
return idx; /* return freqvals index */
}
/* simple insertion sort */
void inssort (int *arr, int size)
{
for (int i = 0; i < size; i++)
for (int j = i - 1; j >= 0; j--)
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
else
break;
}
int main (void) {
int data[] = { 8, 9, 5, 6, 7, 6, 7, 9 }, /* unsorted data */
dsize = sizeof data / sizeof *data, /* number of elements */
*freqvals = new int[dsize], /* allocate for freqvals */
freq = 0, /* frequency from user */
nfreq = 0; /* return from function */
for (int i = 0; i < dsize; i++) /* zero the freqvals array */
freqvals[i] = 0;
inssort (data, dsize); /* sort data */
cout << "enter requested frequency: "; /* prompt for frequency input */
if (!(cin >> freq)) {
cerr << "error: invalid input.\n";
return 1;
}
/* call function saving return in nfreq */
if ((nfreq = countfreq (data, dsize, freqvals, freq))) {
cout << "the values matching requested frequency were: ";
for (int i = 0; i < nfreq; i++) /* output csv */
if (i)
cout << ", " << freqvals[i];
else
cout << freqvals[i];
cout << "\n";
}
else /* otherwise, no values matched requested frequency */
cout << "no values appear with the requested frequency.\n";
delete[] freqvals; /* free allocated memory */
}
除了使用 cin/cout 和 new/delete 之外,没有任何 C++ 固有的解决方案,fgets/printf 和 malloc/free 也可以使用 stdio.h 和 stdlib.h 代替 iostream .
如果您确实能够为您的数组使用vector,那么您可以利用C++ 提供的细节。您可以简单地 sort 您的向量并通过仅传递对您的 data 向量的常量引用和要查找的频率来简化函数参数。您可以从您的函数返回一个向量,然后简单地测试返回向量的.size() 以确定是否找到任何具有请求频率的值。逻辑是相同的,你必须使用的工具更方便一些,例如
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> countfreq (vector<int>& data, int freq)
{
vector<int> freqvals; /* vector to return */
int dsize = data.size(), /* size of initial check */
last = data.front(), /* first value in data */
count = 0, /* count flag for range loop */
seq = 1; /* sequential values counted */
if (freq <= 0 || freq > dsize) /* is freq valid? */
return freqvals;
for (auto& i : data) { /* loop over data */
if (!count) { /* if first iter, set flag continue */
count++;
continue;
}
if (last == i) /* if current equals last */
seq++; /* increment sequential count */
else { /* otherwise */
if (seq == freq) /* if sequential count == freq */
freqvals.push_back(last); /* add value to vector */
seq = 1; /* reset sequential count = 1 */
}
last = i; /* update last */
}
if (seq == freq) /* handle last value in data */
freqvals.push_back(last);
return freqvals; /* return vector */
}
int main (void) {
vector<int> data = { 8, 9, 5, 6, 7, 6, 7, 9 }; /* unsorted data */
int freq; /* the frequency of values to find */
sort (data.begin(), data.end()); /* sort data */
cout << "enter requested frequency: "; /* prompt for freq input */
if (!(cin >> freq)) {
cerr << "error: invalid input.\n";
return 1;
}
vector<int> frequency = countfreq (data, freq); /* call function */
if (frequency.size()) { /* does return have elements? */
int count = 0; /* flag for output control */
cout << "the values matching requested frequency were: ";
for (auto& i : frequency) /* loop over values in return */
if (count)
cout << ", " << i; /* output as csv */
else {
cout << i;
count = 1;
}
cout << "\n";
}
else /* otherwise, handle no values returned */
cout << "no values appear with the requested frequency.\n";
}
(注意:您提供的值未按顺序用于data的内容)
无论你使用哪个,输出都是一样的,例如
使用/输出示例
$ ./bin/frequency
enter requested frequency: 0
no values appear with the requested frequency.
$ ./bin/frequency
enter requested frequency: 1
the values matching requested frequency were: 5, 8
$ ./bin/frequency
enter requested frequency: 2
the values matching requested frequency were: 6, 7, 9
$ ./bin/frequency
enter requested frequency: 3
no values appear with the requested frequency.
查看一下,如果您还有其他问题,请告诉我。有很多方法可以解决这个问题,所以不要认为你仅限于这种方法。您可以随意拼凑拼图的各个部分。