首页 > 试题广场 >

2024_运动记录_2818

[编程题]2024_运动记录_2818
  • 热度指数:327 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
学校有本科生,硕士,博士,他们都运动,本科为 {B} 学号,硕士为 {S} 学号,博士为 {M} 学号,有若干运动记录,输入为学号,运动形式,开始时间,结束时间,{HHMMSS} 的形式,同一学生可能有多条运动记录。输出按学生种类 {(B,S,M)} 分类,同一种类按运动时间长短从小到大排列。先输出学号,然后是总运动运动时间 (保留 {2} 位小数),运动形式。

输入描述:
第一行输入一个整数 {n(1 \leq n \leq 200)},表示运动记录的数量。
接下来的 {n} 行,每行输入以下内容:
- 学号(字符串,格式为 {Bxxxx, Sxxxx, Mxxxx}
- 运动形式(字符串)
- 开始时间(字符串,格式为 {HHMMSS}
- 结束时间(字符串,格式为 {HHMMSS}


输出描述:
按学生种类 {(B,S,M)} 分类,每种类按运动时间从小到大排列。
对于每个学生,输出:`学号 总运动时间(保留两位小数) 运动形式`(以空格分隔)。
示例1

输入

3
B1001 Running 120000 130000
S2001 Swimming 090000 093000
B1001 Basketball 140000 143000

输出

B1001 1.50 Running Basketball
S2001 0.50 Swimming
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
struct Regarding
{
    string sno;
    string sport;
    int totaltime;

};
bool compare(Regarding lhs,Regarding rhs) {
    return lhs.totaltime < rhs.totaltime;
}
int sportTime(string stime,string etime) {
    int sh = (stime[0] - '0') * 10 + (stime[1] - '0');
    int sm= (stime[2] - '0') * 10 + (stime[3] - '0');
    int ss= (stime[4] - '0') * 10 + (stime[5] - '0');
    int start= sh * 3600 + sm * 60 + ss;//化成秒
    int eh = (etime[0] - '0') * 10 + (etime[1] - '0');
    int em = (etime[2] - '0') * 10 + (etime[3] - '0');
    int es = (etime[4] - '0') * 10 + (etime[5] - '0');
    int end = eh * 3600 + em * 60 + es;
    return end - start;
}
bool contains(vector<string>& sports, string val) {
    for (int i = 0; i < sports.size(); ++i) {
        if (sports[i]==val){
            return true;
        }
    }
    return false;
}// 检查 vector 是否包含某元素
string joinSports(vector<string>& sports) {
    string res;
    for (int i = 0; i < sports.size(); ++i) {
        if (i > 0) {
            res += ' ';
        }
        res += sports[i];
    }
    return res;
}
int main() {
    int n;
    scanf("%d", &n);
    char buf[30] = { 0 };
    map <string, pair<vector<string>, int>> mp;//sno->sprot,totaltime
    for (int i = 0; i < n; ++i) {
        string sno, sport, beginTime, endTime;
        scanf("%5s", buf);
        sno = buf;
        scanf("%s", buf);
        sport = buf;
        scanf("%6s", buf);
        beginTime = buf;
        scanf("%6s", buf);
        endTime = buf;
        int time=sportTime(beginTime, endTime);
        if (mp.count(sno) != 0) {
            mp[sno].second += time;
            if (contains(mp[sno].first, sport) == false) {
                mp[sno].first.push_back(sport);
            }
        }
        else
        {
            vector<string> sports;
            sports.push_back(sport);
            mp[sno] = make_pair(sports, time);
        }
    }
    vector<Regarding> bvec, svec, mvec;
    map <string, pair<vector<string>, int>>::iterator it;
    for (it = mp.begin(); it!=mp.end(); ++it) {
        Regarding c;
        c.sno = it->first;
        c.sport =joinSports(it->second.first);
        c.totaltime = it->second.second;
        char type = c.sno[0];
        if (type == 'B') {
            bvec.push_back(c);
        }
        else if (type == 'S') {
            svec.push_back(c);
        }
        else
        {
            mvec.push_back(c);
        }
    }
    sort(bvec.begin(), bvec.end(), compare);
    sort(svec.begin(), svec.end(), compare);
    sort(mvec.begin(), mvec.end(), compare);
    for (int i = 0; i < bvec.size(); ++i) {
        printf("%s %.2f %s\n", bvec[i].sno.c_str(), bvec[i].totaltime / 3600.0, bvec[i].sport.c_str());
    }
    for (int i = 0; i < svec.size(); ++i) {
        printf("%s %.2f %s\n", svec[i].sno.c_str(), svec[i].totaltime / 3600.0, svec[i].sport.c_str());
    }
    for (int i = 0; i < mvec.size(); ++i) {
        printf("%s %.2f %s\n", mvec[i].sno.c_str(), mvec[i].totaltime / 3600.0, mvec[i].sport.c_str());
    }
    return 0;
}
发表于 2026-03-14 18:01:02 回复(0)