我最近正在研究一组数字,我有大量垂直存储的数字序列。我正在计算哪个数字出现了多少次,然后我选择了命中次数高的数字。
我的代码如下所示:
// select the most repeated number
int countArray[1000];
seq_len = i;
for (i=0; i < seq_len; i++) {
scanf("%d", page_seq[i]);
countArray[i] = -1;
}
for(i=0; i < seq_len; i++) {
count = 1;
for( j = i+1; j < seq_len; j++) {
if(page_seq[i] == page_seq[j]) {
countArray[j] = 0;
count++;
}
}
if(countArray[i] != 0) {
countArray[i] = count;
}
}
//print count of each page frequency
for(i=0; i < seq_len; i++) {
if(countArray[i] != 0) {
printf("page %d : count %d\n", page_seq[i], countArray[i]);
}
}
//distinguish most frequently readed page and print its read count
int most_freq;
int maxPage=-1, maxPageIdx=-1;
int pinned_page;
for(i=0; i < seq_len; i++) {
if(countArray[i] != 0) {
if(page_seq[i] > maxPage) {
maxPage = page_seq[i];
maxPageIdx = i;
}
}
for(i=0; i < seq_len; ++i) {
if(countArray[0] < countArray[i])
countArray[0] = countArray[i];
scanf("%d",countArray[i]);
most_freq = countArray[0];
}
for (i =0; i< seq_len; i++) {
if(countArray[i] > most_freq){
most_freq = countArray[i];
count++;
}
}
}
pinned_page = page_seq[maxPage];
printf("pinned page %d\n", pinned_page);
它适用于简单的 (1,2,3,4) 数字,我得到我的每页频率计数,我看到最高计数的数字,但在 ex。
139595776
139538432
139534336
139632640
这些数字并没有显示我的最高计数,而是我得到 SEGMENTATION FAULT(core dumped)
任何帮助将不胜感激