1968 Problem E 众数

389 篇文章 1 订阅
144 篇文章 2 订阅

问题 E: 众数
时间限制: 1 Sec 内存限制: 32 MB
献花: 45 解决: 29
[献花][花圈][TK题库]
题目描述
输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的众数,则输出权值较小的那一个)。
输入
测试数据有多组,每组输入20个1-10之间的数。
输出
对于每组输入,请输出1-10中的众数。

注意如果存在一样多次数的众数,则输出权值较小的那一个。

样例输入
8 9 6 4 6 3 10 4 7 4 2 9 1 6 5 6 2 2 3 8
样例输出
6

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <ctime>
using namespace std;


typedef struct
{
    int n;
    int c;
}Num;

bool cmp(Num a, Num b)
{
    if (a.c != b.c) return a.c > b.c;
    return a.n < b.n;
}

int main()
{
#ifdef _DEBUG
    //freopen("data.txt", "r+", stdin);
    fstream cin("data.txt");
#endif // _DEBUG

    Num num[11];
    int tmp;

    while (cin >> tmp)
    {
        for (int i = 1; i <= 10; ++i)
        {
            num[i].n = i;
            num[i].c = 0;
        }

        ++num[tmp].c;
        for (int i = 1; i < 20; ++i)
        {
            cin >> tmp;
            ++num[tmp].c;
        }

        sort(num + 1, num + 11, cmp);

        cout << num[1].n << endl;
    }





#ifdef _DEBUG
    cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}
/**************************************************************
    Problem: 1968
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:4 ms
    Memory:1708 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值