Hdu 5038 Grade(2014 ACM/ICPC Asia Regional Beijing Online 1007)

来源:互联网 发布:网易乐乎 编辑:程序博客网 时间:2024/05/16 05:05

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

题目的意思, 给你公式,求出每个蘑菇的grade,求这些grade的众数。。Mode,众数,表示英语又被鄙视了。

表示,自己很弱,不知道众数的定义。。。

百度之。。。

一般来说,一组数据中,出现次数最多的数就叫这组数据的众数。
例如:1,2,3,3,4的众数是3。
但是,如果有两个或两个以上个数出现次数都是最多的,那么这几个数都是这组数据的众数。
例如:1,2,2,3,3,4的众数是2和3。
还有,如果所有数据出现的次数都一样,那么这组数据没有众数。
例如:1,2,3,4,5没有众数。
但是,1,1,1,1的众数是1。
明白了 这些个定义就好了。
简单来说,就是出现频率最高的那些数。(如果频率都是一样的,也就没有什么最高与最低了,除非就一种数)。
这就很简单了。。
Code:
#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>using namespace std;const int N = 1e4 + 5;int hash[N];struct Node{    int x, cnt;    Node(){};    Node(int a, int b){        x = a; cnt = b;    }}p[N];bool cmp(Node a, Node b){    if(a.cnt > b.cnt) return true;    else if(a.cnt == b.cnt && a.x < b.x) return true;    return false;}int main(){    int T, k = 1;    cin >> T;    while(T --){        memset(hash, 0, sizeof(hash));        int n, x;        cin >> n;        for(int i = 1; i <= n; i ++){            cin >> x;            hash[10000 - (100 - x) * (100 - x)] ++;        }        int top = 0;        for(int i = 0; i <= 10000; i ++){            if(hash[i] != 0){                Node tmp(i, hash[i]);                p[top ++] = tmp;            }        }        sort(p, p + top, cmp);        printf("Case #%d:\n", k ++);        if(p[0].cnt == p[top - 1].cnt){            if(p[0].x == p[top - 1].x){                printf("%d\n", p[0].x);            }            else puts("Bad Mushroom");        }        else {            printf("%d", p[0].x);            for(int i = 1; i < top; i ++){//                printf("%d\n", p[i].cnt)                if(p[i].cnt == p[0].cnt){                    printf(" %d", p[i].x);                }                else break;            }            printf("\n");        }    }    return 0;}

对于这种水题,不能很快的A掉需要好好的反思。。

0 0
原创粉丝点击