1 条题解

  • 0
    @ 2026-3-4 14:18:02

    文字教学

    这道题数据范围特殊(候选人编号仅1到n,n≤999),用计数排序最快:

    1. 开一个计数数组 cntcnt[i] 表示编号 i 的选票张数。
    2. 遍历所有选票,统计每个编号的票数。
    3. 从1到n依次输出,每个编号输出 cnt[i] 次,直接得到有序结果。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    int cnt[1005];
    
    int main() {
        int n, m, x;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < m; ++i) {
            scanf("%d", &x);
            cnt[x]++;
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j < cnt[i]; ++j) {
                printf("%d ", i);
            }
        }
        printf("\n");
        return 0;
    }
    
    • 1

    信息

    ID
    4688
    时间
    1000ms
    内存
    125MiB
    难度
    3
    标签
    递交数
    3
    已通过
    3
    上传者