HDOJ 5038 Grade

本文详细解析了HDU 5038问题的求解思路,通过输入数据转化为S并计数,最终确定出现次数最多的S。注意点包括特殊情况下输出单一S值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:根据题给公式计算s,输出s数量最多的数。如果个数相同,按升序输出。当每个s的数量都相等时输出BadMushroom。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5038

思路:每个输入的数据转化为s,并计数,排序得到数量最多 的S的值,并输出。

注意点:当s仅为一种值的时候输出s。

以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
116954492014-09-21 19:04:55Accepted5038890MS344K1970BG++luminous11

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;

struct node {
    int n;
    int cnt;
}num[205];

set<int> p;

int calc ( int i )
{
    p.insert ( 10000 - (  i ) * ( i ) );
}

void init ( const int &n )
{
    int k;
    for ( int i = 0; i < 205; i ++ )
    {
        num[i].n = i;
        num[i].cnt = 0;
    }
    for ( int i = 0; i < n; i ++ )
    {
        cin >> k;
        if ( k <= 100 )
        {
            num[100 - k].cnt ++;
        }
        else
        {
            num[k - 100].cnt ++;
        }
    }
}

bool cmp ( const node &a, const node & b )
{
    return a.cnt > b.cnt ? 1 : 0;
}

int main()
{
    ios::sync_with_stdio( false );
    int ncase;
    cin >> ncase;
    //int num[105];
    //while ( t -- )
    for ( int t = 1; t <= ncase; t ++ )
    {
        cout << "Case #" << t << ":" << endl;
        p.clear();
        int flag = 0;
        int n;
        int k;
        cin >> n;
        memset ( num, 0, sizeof ( num ) );
        init ( n );
        sort ( num, num + 100, cmp );
        int tmp = num[0].cnt;
        if ( num[1].cnt == 0 )
        {
            flag = 1;
        }
        for ( int i = 0; i < 205; i ++ )
        {
             if ( num[i].cnt == tmp )
             {
                 calc( num[i].n );
             }
             if ( num[i].cnt != tmp && num[i].cnt != 0 )
             {
                 flag = 1;
             }
        }
        set<int>::iterator it = p.begin();
        if ( ! flag )
        {
            cout << "Bad Mushroom" << endl;
        }
        else
        {
            cout << *it;
            it ++;
            for ( ; it != p.end(); it ++ )
            {
                cout << ' ' << *it;
            }
            cout << endl;
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值