UVA10370 【Above Average】

题目大意:给出\(N\)个学生以及他们的分数,求分数高于平均分的学生所占的百分比。

算法分析:模拟,枚举或者乱搞

很简单的模拟,没什么可说的,注意一下中间结果要储存成\(double\)类型,另外就是做\(UVA\)的题目一定要注意的事\(:\)输入和输出格式\(……\)

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define D double

int T, n;
D a[1007];

template<class T>
inline T read(T &x) {
    x = 0; int w = 1, ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - 48; ch = getchar();}
    return x *= w;
} // 来份快读

int main() {
    while (scanf("%d", &T) != EOF) {  
        while (T--) {
            read(n);
            for (int i = 1; i <= n; i++)
                read(a[i]);
            D sum = 0.0;  // 中间结果一定要用double来存
            for (int i = 1; i <= n; i++)
                sum += a[i];
            D average = sum / n; // 同上
            int cnt = 0;
            for (int i = 1; i <= n; i++)
                if (a[i] > average)
                    ++cnt;
            printf("%.3lf%%\n", cnt * 100.0 / n);  // 注意一定是乘100.0,否则会输出0
        }
    }
    return 0;
}

完结撒花(๑╹ヮ╹๑)ノ

posted @ 2019-10-25 14:55  Hydrogen_Helium  阅读(113)  评论(0编辑  收藏  举报