-
Notifications
You must be signed in to change notification settings - Fork 1
/
众数 malloc
49 lines (46 loc) · 1.2 KB
/
众数 malloc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//问题描述:
//给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数,其中重数最大的元素称为众数。
//例如,S={1,2,2,2,3,5},则S的众数是2,其重数为3。
//编程任务:
//对于给定的由n 个正整数组成的多重集合S,编程计算S 的众数及其重数。
#include <stdio.h>
#include <stdlib.h>
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
int main()
{
int n = 0;
scanf("%d", &n);
int* arr = (int*)malloc(sizeof(int) * n);//申请一块内存
//存入数据
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), cmp_int);
int left = 0;
int right = 0;
int mode = 0;
int mode_sum = 0;
for (right = 0; right < n; right++)
{
if (arr[left] != arr[right])
{
if (right - left > mode_sum)
{
mode_sum = right - left;
mode = arr[left];
}
left = right;
}
}
if (right - left > mode_sum)//数组最末尾的相同序列没算进来,11223333,left在第一个3出,right在最后一个3处,无法进入for循环里的if语句
{
mode_sum = right - left;
mode = arr[left];
}
printf("%d\n%d", mode, mode_sum);
return 0;
}