排序1141


#include <iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<unordered_map>

#include<vector>

#include<algorithm>

#include<cctype>

using namespace std;



unordered_map<string, int> sNum;

unordered_map<string, int> sScore;

const int maxv = 1e5 + 5;

int n;

struct node{

       string school;

       int scoreT, num;

};



bool cmp(node a, node b){

       if(a.scoreT != b.scoreT) return a.scoreT > b.scoreT;

       else if(a.num != b.num) return a.num < b.num;

       else return a.school < b.school;

}







int main(){

       scanf("%d",&n);

       string id,schoolName;



       for(int i = 0; i < n; i++){

              double score;

              cin>>id;

              scanf("%lf",&score);

              cin>>schoolName;

              for(int i = 0; i< schoolName.length();i++){

                     if(schoolName[i] >= 'A' && schoolName[i] <= 'Z'){

                            schoolName[i] = tolower(schoolName[i]);//学习名称变小写

                     }

              }

              if(id[0] == 'B'){

                     score = score / 1.5;

              }else if(id[0] == 'T'){

                     score = score * 1.5;

              }



              sNum[schoolName]++;

              sScore[schoolName] += score;

       }



       vector<node> ans;

       //unordered_map<string, int> ::iterator it;
       //用auto 就不要写上面的这一行了

       for(auto it = sNum.begin(); it != sNum.end();it++){

//                     node temp = new node;

//                     temp.school = it->first;

//                     temp.scoreT = sScore[it->first];

//                     temp.num = sNum[it->first];

//简化为以下一句:

              ans.push_back(node{it->first,(int)sScore[it->first],sNum[it->first] });

       }



       sort(ans.begin(), ans.end(),cmp);

       printf("%d\n", (int)ans.size());//学校个数

       int rank = 0, now = -1;//排名

       for(int i = 0; i < ans.size(); i++){

              if(ans[i].scoreT != now) rank = i+1;

              now = ans[i].scoreT;

              printf("%d ", rank);

              cout<<ans[i].school;

              printf(" %d %d\n", ans[i].scoreT, ans[i].num);

       }



       return 0;

}

不熟悉的地方:

1.unordered_map的使用:

要加头文件 #include<unordered_map>
 

其实可以理解为 下标 可以为字符串的 数组 

unordered_map<string, int> sNum;
unordered_map<string, int> sScore;

 

这样sNum[name] ++;实现计数功能

sScore[name]实现 统计分数 

而用 unordered_map 会比map快一点

当超时的时候可以考虑换成这个 出现调用头文件错误的时候 ; 解决方法 https://blog.csdn.net/liu_feng_zi_/article/details/93714023

2. 大小写转换

             

              cin>>schoolName;
              for(int i = 0; i< schoolName.length();i++){
                     if(schoolName[i] >= 'A' && schoolName[i] <= 'Z'){
                            schoolName[i] = tolower(schoolName[i]);//学习名称变小写
                     }
              }

tolower();

toupper();

要加头文件 #include<cctype>
 

//

 

3. double 的输入用%1f

是1 (1233 的 1)

不是l !!!

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值