C语言 C中使用数组的序列中最频繁的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22398987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most frequent element in a sequence using arrays in C
提问by asrm
I'm doing an online course on "Programming, Data Structure & Algorithm". I've been given an assignment to "find the most frequent element in a sequence using arrays in C (with some constraints)". They've also provided some test-cases to verify the correctness of the program. But I think I'm wrong somewhere.
我正在上一门关于“编程、数据结构和算法”的在线课程。我被赋予了“使用 C 中的数组(有一些限制)在序列中找到最频繁的元素”的任务。他们还提供了一些测试用例来验证程序的正确性。但我想我在某处错了。
Here's the complete question from my online course.
这是我在线课程中的完整问题。
INPUT
Input contains two lines. First line in the input indicates N, the number of integers in the sequence. Second line contains N integers, separated by white space.
OUTPUT
Element with the maximum frequency. If two numbers have the same highest frequency, print the number that appears first in the sequence.
CONSTRAINTS
1 <= N <= 10000
The integers will be in the range
[-100,100].
输入
输入包含两行。输入中的第一行表示 N,即序列中整数的数量。第二行包含 N 个整数,用空格分隔。
输出
具有最大频率的元素。如果两个数字具有相同的最高频率,则打印序列中最先出现的数字。
约束
1 <= N <= 10000
整数将在范围内
[-100,100]。
And here's the test cases.
这是测试用例。
Test Case 1
测试案例 1
Input:
输入:
5
1 2 1 3 1
Output:
输出:
1
Input:
输入:
6
7 7 -2 3 1 1
Output:
输出:
7
And here's the code that I've written.
这是我写的代码。
#include<stdio.h>
int main()
{
int counter[201] = {0}, n, i, input, maximum = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++) {
scanf("%d", &input);
if(input < -100 && input < 100)
++counter[input];
}
maximum = counter[0];
for (i = 1; i < 201; i++) {
if (counter[i] > maximum) {
maximum = counter[i];
}
}
printf("%d", maximum);
return 0;
}
Please tell me where I'm wrong. Thank you.
请告诉我我错在哪里。谢谢你。
EDIT:
编辑:
I've modified the code, as suggested by @zoska. Here's the working code.
我已经按照@zoska 的建议修改了代码。这是工作代码。
#include<stdio.h>
int main()
{
int counter[201] = {0}, n, i, input, maximum = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++) {
scanf("%d", &input);
if(input < 100 && input > 0)
++counter[input + 100];
else
++counter[input];
}
maximum = counter[0];
for (i = 0; i < 201; i++) {
if (counter[i] > maximum) {
maximum = i - 100;
}
}
printf("%d", maximum);
return 0;
}
回答by zoska
Additionally to problem pointed out by Paul R is:
除了 Paul R 指出的问题之外,还有:
You are printing maximum occurrences of number, not the number itself.
您正在打印数字的最大出现次数,而不是数字本身。
You're going to need another variable, which will store the number with maximum occurences. Like :
您将需要另一个变量,它将存储出现次数最多的数字。喜欢 :
maximum = count[0];
int number = -100;
for (i = 0; i < 201; i++) {
if (counter[i] > maximum) {
maximum = counter[i];
number = i - 100;
}
}
printf("number %d has maximum occurences: %d", number, maximum);
Also you should iterate through an array from 0 to size-1: So in all cases of your loops it should be :
此外,您应该遍历从 0 到大小为 1 的数组:因此,在所有循环情况下,它应该是:
for(i = 0; i < 201; i++)
Otherwise you won't be using count[0] and you will only have a range of -99...100.
否则您将不会使用 count[0] 并且您将只有 -99...100 的范围。
回答by user207064
Try below code
试试下面的代码
#include<stdio.h>
int main()
{
int counter[201] = {0}, n, i, input, maximum = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++) {
scanf("%d", &input);
if(input >= -100 && input <= 100)
++counter[input + 100];
}
maximum = counter[0];
int index = 0;
for (i = 0; i < 201; i++) {
if (counter[i] >= maximum) {
index = i;
maximum = counter[i];
}
}
printf("number %d occured %d times\n", index-100, maximum);
return 0;
}
回答by angad.stjudes
I would prefer checking in one loop itself for the maximum value just so that the first number is returned if i have more than one element with maximum number of occurances. FInd the code as:
我更喜欢在一个循环本身中检查最大值,以便在我有多个具有最大出现次数的元素时返回第一个数字。找到代码如下:
#include<stdio.h>
int main()
{
int n,input;
scanf("%d",&n);
int count[201] ={0};
int max=0,found=-1;
for(int i=0;i<n;i++)
{
scanf("%d",&input);
count[input+100]++;
if(max<count[input+100])
{
max= count[input+100];
found=input;
}
}
printf("%d",found);
return 0;
}
回答by user3449117
But, there is also one condition that if the number of occurance are same for two numbers then the number which appers first in sequence should appear.
但是,还有一个条件,如果两个数字出现的次数相同,则应该出现按顺序首先出现的数字。

