题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184
题意:
给定几个学生的PAT分数和学校,给这些学校学生的PAT总分排序。
思路:
库函数tolower()和toupper()可以分别把字符串转换为都是小写字母和都是大写字母。
这道题要注意的是,因为是加权的总分,算的时候应该用double。但是题目中有说,总分取加权之后的整数部分,全部加完后要转成int进行比较。
1 //#include<bits/stdc++> 2 #include<stdio.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<stdlib.h> 7 #include<queue> 8 #include<map> 9 #include<stack> 10 #include<set> 11 12 #define LL long long 13 #define ull unsigned long long 14 #define inf 0x7f7f7f7f 15 16 using namespace std; 17 18 const int maxn = 1e5 + 5; 19 int n; 20 struct school{ 21 string name; 22 int num; 23 double sco; 24 int rnk; 25 }sch[maxn]; 26 set<string>school_name; 27 set<string>::iterator iter; 28 map<string, int>schoolid; 29 30 bool cmp(school &a, school &b) 31 { 32 if((int)a.sco == (int)b.sco) 33 if(a.num == b.num)return a.name < b.name; 34 else return a.num < b.num; 35 else return (int)a.sco > (int)b.sco; 36 } 37 38 struct student{ 39 string name; 40 double sco; 41 string sch; 42 }stu[maxn]; 43 44 int main() 45 { 46 scanf("%d", &n); 47 for(int i = 0; i < n; i++){ 48 cin>>stu[i].name>>stu[i].sco>>stu[i].sch; 49 if(stu[i].name[0] == 'T')stu[i].sco *= 1.5; 50 else if(stu[i].name[0] == 'B')stu[i].sco /= 1.5; 51 transform(stu[i].sch.begin(), stu[i].sch.end(), stu[i].sch.begin(), ::tolower); 52 school_name.insert(stu[i].sch); 53 } 54 int id = 0; 55 for(iter = school_name.begin(); iter != school_name.end(); iter++){ 56 string s = *iter; 57 sch[id].name = s; 58 schoolid[s] = id++; 59 } 60 61 for(int i = 0; i < n; i++){ 62 int sid = schoolid[stu[i].sch]; 63 sch[sid].num++; 64 sch[sid].sco += stu[i].sco; 65 } 66 67 sort(sch, sch + id, cmp); 68 int rnk = 0, tie = 0; 69 printf("%d ", id); 70 for(int i = 0; i < id; i++){ 71 sch[i].sco = (int)sch[i].sco; 72 if(i != 0 && sch[i].sco == sch[i - 1].sco){ 73 tie++; 74 } 75 else{ 76 rnk += tie + 1; 77 tie = 0; 78 } 79 cout<<rnk<<" "<<sch[i].name<<" "<<sch[i].sco<<" "<<sch[i].num<<endl; 80 } 81 82 return 0; 83 }