现在的位置: 首页 > 综合 > 正文

SDUT1480

2014年06月12日 ⁄ 综合 ⁄ 共 547字 ⁄ 字号 评论关闭

数据结构实验:哈希表


Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

 在n个数中,找出出现次数最多那个数字,并且输出出现的次数。如果有多个结果,输出数字最小的那一个。

输入

 单组数据,第一行数字n(1<=n<=100000)。
接下来有n个数字,每个数字不超过100000000

输出

 出现次数最多的数字和次数。

示例输入

3
1 1 2

示例输出

1 2

 

 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
    return x<y;
}
int main()
{
    int n,a[100001],k=0,count=1,t;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++)
    {
        if(a[i]==a[i+1])
        {

            count=count+1;
        }
        else  {if(count>k) k=count,t=a[i];count=1;}
    }
    printf("%d %d\n",t,k);
    return 0;
}

 

【上篇】
【下篇】

抱歉!评论已关闭.