HDU2093 考试排名

本文深入探讨了一个学生竞赛排名系统的设计与实现,重点讲解了如何通过处理字符串输入、计算每位参赛者的得分和AC题目数量来生成公正的排名列表。文章详细介绍了使用C++进行数据结构设计、算法优化和输出格式控制的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题主要考察字符串处理和输出格式

printf("%5d") 是左对齐 固定输出5个字符

printf("%-5d") 是右对齐 固定输出5个字符

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <ctype.h>
using namespace std;

#define NAMESIZE 10
#define MAXSIZE 100

struct student{
    char name[20];
    int numAC = 0;  //ac题目数
    int score = 0;  //时间分,越低越好
};

bool cmpAC(struct student s1, struct student s2){
    if(s1.numAC != s2.numAC)
        return s1.numAC > s2.numAC;
    else
        return s1.score < s2.score;
}


int  main(){
    vector<student> que;
    int n, s;
    char a[10];
    struct student st;
    scanf("%d %d\n", &n, &s);
    while(scanf("%s", st.name) != EOF){
        st.score = 0;
        st.numAC = 0;
        for(int i=0; i<n; i++){
            scanf("%s", a);
            int index = 0;
            if(a[index] == '-') continue;   //负数不处理
            else if(a[index] == '0') continue;  //0也不处理
            else if(a[index] >= '1' && a[index] <= '9'){
                st.numAC++;
                int num = 0;
                int error = 0;
                while(a[index]>='0' && a[index]<='9' && a[index] != '('){
                    num = num*10 + a[index]-'0';
                    index++;
                }
                if(a[index] == '('){
                    index++;
                    while(a[index]>='0' && a[index]<='9' && a[index] != ')'){
                        error = error*10 + a[index]-'0';
                        index++;
                    }
                }
                num = num + error*s;
                st.score += num;
            }
        }
        que.push_back(st);
    }
    sort(que.begin(), que.end(), cmpAC);
    vector<student>::iterator it = que.begin();
    for(; it != que.end(); it++){
        printf("%-10s %2d %4d\n", it->name, it->numAC, it->score);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值