PAT甲级 1141 PAT Ranking of Institutions (25分) map/C++

1141 PAT Ranking of Institutions (25分)

题目大意:给定考生ID,分数,学校,要求对各个学校总分进行排名。

tips:

  1. 学校code需要全部转换为小写
  2. 按照分数排名,如果分数相同,按照考生人数从小到大排列,如果考生人数相同,则按照字母表顺序排列。
  3. 分数需要到最后再转化为int(测试点5)
  4. 如果用map超时,可以考虑使用unordered_map(不过我用map并没有超时)
#include<iostream>                  //输入输出流头文件
#include<algorithm>                 //C++标准模板库的函数
#include<map>                       //map映射容器
#include<vector>                    //变长数组容器
#include<string>                    //C++string类
using namespace std;                //标准命名空间
int n;
struct sc{
	int ns;
	double totalscore;
};
bool cmp(pair<string,sc> a,pair<string,sc> b){
	return (int)a.second.totalscore!=(int)b.second.totalscore?a.second.totalscore>b.second.totalscore:(a.second.ns!=b.second.ns?a.second.ns<b.second.ns:a.first<b.first);
}
int main(){                         //主函数
#ifdef ONLINE_JUDGE                 //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else    
   freopen("1.txt", "r", stdin);   //从1.txt输入数据
#endif 
  cin>>n;
 map<string ,sc> m;
  for(int i=0;i<n;i++){
	  string id,school;
	  double score;
	  cin>>id>>score>>school;
	  for(int j=0;j<school.size();j++){
		  if(school[j]<'a') school[j]=school[j]+32;//变为小写字母
	  }
	  string level=id.substr(0,1);//等级
	  if(level=="B")score/=1.5;
	  else if(level=="T")score*=1.5;
	  m[school].ns++;
	  m[school].totalscore+=score;
  }
    vector<pair<string,sc>> v(m.begin(), m.end());//存到vector中,按分数排序
  cout<<m.size()<<endl;
 sort(v.begin(),v.end(),cmp);
 int i=1, lastscore=v[0].second.totalscore;//i为排名
  cout<<i<<' '<<v[0].first<<' '<<(int)v[0].second.totalscore<<' '<<v[0].second.ns<<endl;
  for(int j=1;j<v.size();j++){
	  if((int)v[j].second.totalscore==lastscore) cout<<i;
	  else {cout<<j+1;i=j+1;}
	  cout<<' '<<v[j].first<<' '<<(int)v[j].second.totalscore<<' '<<v[j].second.ns<<endl;
	  lastscore=v[j].second.totalscore;
  }
    return 0;                       //返回0,如果不返回0,PAT会报错
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值