【机试备考】Day29-众数 | 数学问题

题目

BUPT 2014 计算机 ProblemA(oj)
给定一个长度为N的非降数列,求数列中出现次数最多的数。如果答案不唯一,输出其中最小的数。

用时:17min

输入描述

输入数据第一行是一个整数T(1≤T≤100),表示测试数据的组数。对于每组测试数据:
第一行是一个正整数N(1≤N≤100),表示数列长度。
第二行有N个整数,整数之间用空格隔开,所有的整数都不超过105,表示这个数列。

输出描述

对于每组测试数据,输出一个整数。

示例

输入

2
4
1 1 1 2
5
1 1 2 2 3

输出

1
1

题解

#include <bits/stdc++.h>
#define M 100005
using namespace std;
int main(){
    
    
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)
    {
    
    
        int n;
        cin>>n;
        int num[M]={
    
    0};
        while(n--)
        {
    
    
            int a;
            cin>>a;
            num[a]++;
        }
        int m=0,max=1;
        for(int j=M-1;j>0;j--)
        {
    
    
            if(num[j]>=max)
            {
    
    
                m=j;
                max=num[j];
            }
        }
        cout<<m<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43417265/article/details/114399543
今日推荐