bit 1006 The most frequent number

The most frequent number

时间限制: 1秒  内存限制: 64M

Problem Description

Given n integers, can you find the most frequent one? Now let’s think about this problem.

Input

This problem contains multiple test cases.

For each test case, the first line is an integer n (1 <= n <= 100000), then the next line has n integers, every of which is between -10^9 to 10^9.

The input is ended by EOF.

Output

For each test case, please output the most frequent number. If there is more than one solution, please output the minimum one.

Sample Input

6

1 2 2 2 3 5

3

-1 -1 -1

Sample Output

2

-1


排一次序,遍历一遍。ac。

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b){
	return (*(int *)a) - (*(int *)b);
}
int main(int argc, char *argv[])
{
	int a[100100]; 
	int n;
	int tem,tem_befor;
	int max,max_befor;
	//FILE *fp;
	//fp = freopen("in.txt","r",stdin);

	while(~scanf("%d",&n)){
		for(int i = 0;i < n;++i){
			scanf("%d",&a[i]);
		}
		qsort(a,n,sizeof(int),cmp);
		tem = a[0];
		tem_befor = a[0];
		max = 1;
		max_befor = 0;
		
		for(int i = 1; i < n;++i){
			if(tem == a[i]){
				++max;
			}else{
				if(max > max_befor){
					max_befor = max;
					tem_befor = tem;
					
				}
				tem = a[i];
				max = 1;
			}
		}
		printf("%d\n",max > max_befor ? tem:tem_befor);
		
	}
	return 0;
} 


你可能感兴趣的:(bit)