[PAT乙级]1085 PAT单位排行 (25 分)——(STL)

本文探讨了C++编程中如何处理字符串转换、加权评分计算及整数化操作,展示了三次修改代码的过程,重点在于如何正确实现加权平均并取整数部分。通过实例展示了如何使用map和vector结构,以及自定义比较函数来对学校数据进行排序。
摘要由CSDN通过智能技术生成

在这里插入图片描述

三刷

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    string sname;
    double avscore;
    int num;
    bool operator<(const node& t) const{
        if(avscore==t.avscore){
            if(num==t.num) return sname<t.sname;
            else return num<t.num;
        }else return avscore>t.avscore;
    }
}temp;
int main(){
    int n;
    cin>>n;
    map<string,struct node> mp;
    vector<struct node> v;
    for(int i=0;i<n;i++){
        string tid,schname;
        int score;
        cin>>tid>>score>>schname;
       for(int j=0;j<schname.size();j++) if(isupper(schname[j])) schname[j]=tolower(schname[j]);
        mp[schname].sname=schname;
        mp[schname].num++;
        if(tid[0]=='B') mp[schname].avscore+=score/1.5;
        else if(tid[0]=='A') mp[schname].avscore+=score;
        else if(tid[0]=='T') mp[schname].avscore+=score*1.5;
    }
    for(auto it=mp.begin();it!=mp.end();it++) v.push_back(it->second);
    for(int i=0;i<v.size();i++){
        v[i].avscore=(int)(v[i].avscore);
	}
    sort(v.begin(),v.end());
    cout<<v.size()<<endl;
    int k=1;//学习
    for(int i=0;i<v.size();i++){
		cout<<k<<" "<<v[i].sname<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
		if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
	}
    return 0;
}

第二次代码

测试点5:
题目要求是:加权分取整数部分,其实是要最后一起取整数,先将他们算过之后保存到double里面,之后遍历一遍数组,给每个学校的分数取整

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    string name;
    int num;
    double avscore;
};
string chg(string sch){
    for(int j=0;j<sch.length();j++){
        if(sch[j]>='A'&&sch[j]<='Z') sch[j]=sch[j]+32;
    }
    return sch;
}
bool cmp(struct node a,struct node b){
    if(a.avscore==b.avscore){
        if(a.num==b.num) return a.name<b.name;
        else return a.num<b.num;
    } 
    else return a.avscore>b.avscore;
}
int main(){
    int n;
    cin>>n;
    map<string,struct node> m;
    for(int i=0;i<n;i++){
        string id,school;
        double score;
        cin>>id>>score>>school;
        m[chg(school)].num++;
        m[chg(school)].name=chg(school);
        if(id[0]=='B') m[chg(school)].avscore+=score/1.5;
        else if(id[0]=='A') m[chg(school)].avscore+=score;
        else if(id[0]=='T') m[chg(school)].avscore+=score*1.5;
    }
    vector<struct node> v;
    for(auto it=m.begin();it!=m.end();it++) v.push_back(it->second);
    for(int i=0;i<v.size();i++) v[i].avscore=(int)(v[i].avscore+1e-8);//这里给每个学校统一取整,(+1e8是为了提高精度,因为C++不能精确的保存小数,所以这里手动加上一个很小的数,在官网提交不加也能过,在AcWing上就过不了)
    sort(v.begin(),v.end(),cmp);
    cout<<v.size()<<endl;
    int k=1;//学习
    for(int i=0;i<v.size();i++){
		cout<<k<<" "<<v[i].name<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
		if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
	}
    return 0;
}

在这里插入图片描述

第一次代码

最后一个测试点总是不过

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    string name;
    int avscore,num;
};
string chg(string sch){
    for(int j=0;j<sch.length();j++){
        if(sch[j]>='A'&&sch[j]<='Z') sch[j]=sch[j]+32;
    }
    return sch;
}
bool cmp(struct node a,struct node b){
    if(a.avscore==b.avscore){
        if(a.num==b.num) return a.name<b.name;
        else return a.num<b.num;
    } 
    else return a.avscore>b.avscore;
}
int main(){
    int n;
    cin>>n;
    map<string,struct node> m;
    for(int i=0;i<n;i++){
        string id,school;
        int score;
        cin>>id>>score>>school;
        m[chg(school)].num++;
        m[chg(school)].name=chg(school);
        if(id[0]=='B') m[chg(school)].avscore+=score/1.5;
        else if(id[0]=='A') m[chg(school)].avscore+=score;
        else if(id[0]=='T') m[chg(school)].avscore+=score*1.5;
    }
    vector<struct node> v;
    for(auto it=m.begin();it!=m.end();it++) v.push_back(it->second);//写笔记这里是it->second;
    sort(v.begin(),v.end(),cmp);
    cout<<v.size()<<endl;
    int k=1;//学习
    for(int i=0;i<v.size();i++){
		cout<<k<<" "<<v[i].name<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
		if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
	}
    return 0;
}

在这里插入图片描述

柳神的代码

#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
#include <unordered_map>
using namespace std;
struct node {
    string school;
    int tws, ns;
};
bool cmp(node a, node b) {
    if (a.tws != b.tws)
        return a.tws > b.tws;
    else if (a.ns != b.ns)
        return a.ns < b.ns;
    else
        return a.school < b.school;
}
int main() {
    int n;
    scanf("%d", &n);
    unordered_map<string, int> cnt;
    unordered_map<string, double> sum;
    for (int i = 0; i < n; i++) {
        string id, school;
        cin >> id;
        double score;
        scanf("%lf", &score);
        cin >> school;
        for (int j = 0; j < school.length(); j++)
            school[j] = tolower(school[j]);
        if (id[0] == 'B')
            score = score / 1.5;
        else if (id[0] == 'T')
            score = score * 1.5;
        sum[school] += score;
        cnt[school]++;
    }
    vector<node> ans;
    for (auto it = cnt.begin(); it != cnt.end(); it++)
        ans.push_back(node{it->first, (int)sum[it->first], cnt[it->first]});
    sort(ans.begin(), ans.end(), cmp);
    int rank = 0, pres = -1;
    printf("%d\n", (int)ans.size());
    for (int i = 0; i < ans.size(); i++) {
        if (pres != ans[i].tws) rank = i + 1;
        pres = ans[i].tws;
        printf("%d ", rank);
        cout << ans[i].school;
        printf(" %d %d\n", ans[i].tws, ans[i].ns);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值