智能成绩表(C++)

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
using namespace std;

int main() {
    int n, m; //人数和科目数量
    while (cin >> n >> m) {
        vector<string> subjects(m);
        //科目录入
        for (int i = 0; i < m; i++) {
            cin >> subjects[i];
        }
        cin.ignore();
        vector<pair<string,vector<int>>> students;
        for (int i = 0; i < n; i++) { //读取每个孩子的分数
            string s;
            getline(cin, s);
            istringstream iss(s);
            string name;
            iss >> name;
            int score;
            vector<int> scores;
            int sum = 0;//计算总得分,放在分数数组最后
            while (iss >> score) {
                sum += score;
                scores.push_back((int)score);
            }
            scores.push_back(sum);//最后录入总分
            students.push_back(make_pair(name, scores));
        }
        string res;
        cin >> res;
        //查找排序基准
        int index  = m;//初始为总分index
        for (int i = 0; i < m; i++) {
            if (subjects[i] == res) {
                index = i;
                break;
            }
        }
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n-i-1;j++){
                if(students[j].second[index]<students[j+1].second[index]||
(students[j].second[index]==students[j+1].second[index]&&students[j].first > students[j+1].first)){
                    //成绩相同,字典小的在前
                    swap(students[j],students[j+1]);
                }
            }
        }
        for(auto it = students.begin();it!=students.end();it++){
            cout << it->first <<' ';
        }
        cout << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值