PAT乙级|1085 PAT单位排行

提交1:超时

解决超时:每次读入时用set判断当前学校是否已插入vector

提交2:最后一个用例没过

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <iomanip>
#define MAX_SIZE 100000
using namespace std;

struct SInfo {
    string school;
    float score;
    int cnt, scoreint;
};

bool cmp(SInfo a, SInfo b) {
    if(fabs(a.score - b.score) <= 0.000001) {
        if(a.cnt == b.cnt) return a.school < b.school;
        return a.cnt < b.cnt;
    }
    return a.score > b.score;
}

int main() {
    int n;
    cin >> n;
    vector<SInfo> sinfo;
    set<string> setSchool;
    while(n--) {
        string strNum, school;
        float score;
        cin >> strNum >> score >> school;
        // 字符串转小写,要加::
        transform(school.begin(), school.end(), school.begin(), ::tolower);
        if(setSchool.find(school) == setSchool.end()) {
            setSchool.insert(school);
            SInfo temp;
            temp.cnt = 1;
            temp.school = school;
            switch (strNum[0]) {
                case 'B':
                    score/=1.5;
                    break;
                case 'T':
                    score*=1.5;
                    break;
                default:
                    break;
            }
            temp.score = score;
            sinfo.push_back(temp);
        } else {
            for(int i = 0; i < sinfo.size(); i++) {
                if(sinfo[i].school == school) {
                    switch (strNum[0]) {
                        case 'B':
                            score/=1.5;
                            break;
                        case 'T':
                            score*=1.5;
                            break;
                        default:
                            break;
                    }
                    sinfo[i].score += score;
                    sinfo[i].cnt ++;
                    break; // 退出循环
                }
            }
        }
    }
    if(sinfo.size() > 1)
        sort(sinfo.begin(), sinfo.end(), cmp);
    cout << sinfo.size() << endl;
    int rank = 1;
    for(int i = 0; i < sinfo.size(); i++) {
        sinfo[i].scoreint = (int)sinfo[i].score;
        if(i && sinfo[i].scoreint != sinfo[i-1].scoreint) rank = i + 1;
        cout<< rank << " " << sinfo[i].school << " " << sinfo[i].scoreint << " " << sinfo[i].cnt << endl;
    }
}

提交3:AC

关键:74行代码,即需先取整再排序(测试点5),上面WA的代码是浮点数排序后再取整输出

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <iomanip>
#define MAX_SIZE 100000
using namespace std;

struct SInfo {
    string school;
    float score;
    int cnt, scoreint;
};

bool cmp(SInfo a, SInfo b) {
    if(a.scoreint == b.scoreint) {
        if(a.cnt == b.cnt) return a.school < b.school;
        return a.cnt < b.cnt;
    }
    return a.score > b.score;
}

int main() {
    int n;
    cin >> n;
    vector<SInfo> sinfo;
    set<string> setSchool;
    while(n--) {
        string strNum, school;
        float score;
        cin >> strNum >> score >> school;
        // 字符串转小写,要加::
        transform(school.begin(), school.end(), school.begin(), ::tolower);
        if(setSchool.find(school) == setSchool.end()) {
            setSchool.insert(school);
            SInfo temp;
            temp.cnt = 1;
            temp.school = school;
            switch (strNum[0]) {
                case 'B':
                    score/=1.5;
                    break;
                case 'T':
                    score*=1.5;
                    break;
                default:
                    break;
            }
            temp.score = score;
            sinfo.push_back(temp);
        } else {
            for(int i = 0; i < sinfo.size(); i++) {
                if(sinfo[i].school == school) {
                    switch (strNum[0]) {
                        case 'B':
                            score/=1.5;
                            break;
                        case 'T':
                            score*=1.5;
                            break;
                        default:
                            break;
                    }
                    sinfo[i].score += score;
                    sinfo[i].cnt ++;
                    break; // 退出循环
                }
            }
        }
    }
    // 取整再排序
    for(int i = 0; i < sinfo.size(); i++)
        sinfo[i].scoreint = (int)sinfo[i].score;
    if(sinfo.size() > 1)
        sort(sinfo.begin(), sinfo.end(), cmp);
    cout << sinfo.size() << endl;
    int rank = 1;
    for(int i = 0; i < sinfo.size(); i++) {
        if(i && sinfo[i].scoreint != sinfo[i-1].scoreint) rank = i + 1;
        cout<< rank << " " << sinfo[i].school << " " << sinfo[i].scoreint << " " << sinfo[i].cnt << endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值