1 条题解

  • 0
    @ 2008-2-11 4:00:53

    练31.1 年龄与疾病

    解题思路

    按 18、35、60 三个上界分类计数,其余归 61 及以上。每类用人数除总人数乘 100 得比例。

    正确性说明

    按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。

    易错点

    四段分别一行,保留两位并带百分号。

    复杂度

    时间 O(n),额外空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        int cnt[4] = {};
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            cnt[x <= 18 ? 0 : x <= 35 ? 1 : x <= 60 ? 2 : 3]++;
        }
        for (int x : cnt)
            cout << fixed << setprecision(2) << 100.0 * x / n << "%\n";
        return 0;
    }
    
    • 1

    信息

    ID
    215
    时间
    1000ms
    内存
    256MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者