【PAT甲级】1075. PAT Judge (25)

来源:互联网 发布:linux 查看登陆用户 编辑:程序博客网 时间:2024/06/05 02:19
注意:如果一名同学提交代码未通过编译,则他这道题成绩打印为0;如果他对某一题从未提交过代码,则他这道题成绩打印为‘-’;如果他提交的所有代码均未通过编译,则不打印他的成绩单。
#include <stdio.h>#include <vector>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;struct Stu{    int id;    int total;    int perfect;};int sco_mat[1000000][6];int n, k, m;void print_score(Stu s);bool cmp(Stu a, Stu b);int main() {    scanf("%d %d %d", &n, &k, &m);    int *p = new int[k + 1];    for (int i = 1; i <= k; i++) {        scanf("%d", &p[i]);    }    int uid, pid, score;    memset(sco_mat, -1, sizeof(sco_mat));    vector<Stu> v;    while (m) {        scanf("%d %d %d", &uid, &pid, &score);        if (sco_mat[uid][0] == -1) {            if (score >= 0) { //给此学生建档                sco_mat[uid][0] = 1;                Stu s;                s.id = uid;                s.total = -1;                sco_mat[uid][pid] = score;                v.push_back(s);            } else if (score == -1) {//编译不通过,并不建档                sco_mat[uid][pid] = 0;            }        } else if (sco_mat[uid][pid] <= score) {            sco_mat[uid][pid] = score;            if (score == -1) sco_mat[uid][pid] = 0;        }        m--;    }    for (int i = 0; i < v.size(); i++) {        int sum = 0;        int perfect = 0;        for (int j = 1; j <= k; j++) {            if (sco_mat[v[i].id][j] != -1) {                sum += sco_mat[v[i].id][j];            }            if (sco_mat[v[i].id][j] == p[j])                perfect++;        }        v[i].total = sum;        v[i].perfect = perfect;    }    if (v.size() == 0) return 0;    sort(v.begin(), v.end(), cmp);    int rank = 1;    printf("1 "); print_score(v[0]);    int pre = 1;    for (int i = 1; i < v.size(); i++) {        if (v[i].total == v[i - 1].total) {            printf("%d ", rank);            pre++;        } else {            printf("%d ", rank += pre);            pre = 1;        }        print_score(v[i]);    }    return 0;}void print_score(Stu s) {    printf("%05d ", s.id);    printf("%d ", s.total);    for (int i = 1; i <= k; i++) {        if (sco_mat[s.id][i] == -1)            printf("-");        else printf("%d", sco_mat[s.id][i]);        if (i < k) printf(" ");    }    printf("\n");    return;}bool cmp(Stu a, Stu b) {    if (a.total > b.total) return true;    else if (a.total == b.total             && a.perfect > b.perfect) return true;    else if (a.total == b.total             && a.perfect == b.perfect             && a.id < b.id) return true;    return false;}