题目1057:众数

来源:互联网 发布:windows桌面经典壁纸 编辑:程序博客网 时间:2024/06/05 09:35
题目描述:

输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的众数,则输出权值较小的那一个)。

输入:

测试数据有多组,每组输入20个1-10之间的数。

输出:

对于每组输入,请输出1-10中的众数。

样例输入:
5 1 5 10 3 5 3 4 8 6 8 3 6 5 10 7 10 2 6 2 
样例输出:
5
#include<iostream>#include<stdio.h>#include<string.h>using std::cin;using std::cout; int lmd(int a,int b){    if(b==0)        return a;    else        return lmd(b,a%b);}int main(){    int hash[11];    int c=0;    int n;    memset(hash,0,sizeof(hash));    while(cin>>n)    {        hash[n]+=1;        c++;        if(c<20)            continue;        int max=1;        for(int c=2;c<11;c++)            if(hash[c]>hash[max])                max=c;        cout<<max<<"\n";        c=0;        memset(hash,0,sizeof(hash));    }}

原创粉丝点击