After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where ID
is a string of 6 characters with the first one representing the test level: B
stands for the basic level, A
the advanced level and T
the top level; Score
is an integer in [0, 100]; and School
is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID
is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where Rank
is the rank (start from 1) of the institution; School
is the institution code (all in lower case); ; TWS
is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5
, where ScoreX
is the total score of the testees belong to this institution on level X
; and Ns
is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS
. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns
. If there is still a tie, they shall be printed in alphabetical order of their codes.
Sample Input:
10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu
Sample Output:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
这题学到新技能 以前一直想用map 的键值对的数据类型成结构体 一直实现不了
后来搜百度 发现只需要重载小于号 其实相当于是排序了真的很不错呢
其次就是 string 类转换成char*类的输出 c_str();
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
比如:最好不要这样:
char* c;
string s="1234";
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
应该这样用:
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
又涨姿势了
有点糟糕就是写了30+分钟
代码:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int num;
double score;
node(){num = 0 ; score = 0;}
bool operator <(const node &a)const{
if(this->num < a.num)//返回rank值小的
return true;
return false;
}
};
struct last
{
int num;
int score;
string school;
}s[100005];
bool cmp(last a,last b){
if(a.score == b.score){
if(a.num == b.num){
return a.school < b.school;
}else
{
return a.num < b.num;
}
}else return a.score > b.score;
}
int main()
{
int n;
scanf("%d",&n);
char str[10],name[10];
double sc;
map<string, node>mp;//int 就来表示有多少人好了
for(int i = 1; i <= n; i++)
{
scanf("%s %lf %s",name,&sc,str);
int size = strlen(str);
for(int j = 0 ;j < size ;j++)
{
if(str[j]>='A' && str[j] <= 'Z')
str[j] += 32;
}//表示学校名字.......
node tmp = mp[str];
if(name[0] == 'A'){
tmp.score += sc;
}else if(name[0] == 'B')
{
tmp.score += sc/1.5;
}else
tmp.score += sc*1.5;
tmp.num++;
mp[str] = tmp;
}
map<string,node>::iterator it = mp.begin();
int now = 0;
while(it != mp.end()){
s[++now].school = it->first;
s[now].num = it->second.num;
s[now].score = (int)it->second.score;
it++;
}//接下来就是定义rank值了
sort(s+1,s+1+now,cmp); //排序
s[0].score = s[1].score;
int rank = 1;
int cnt = 0;
printf("%d\n",now); //利用rank和cnt来设定对应的值
for(int i = 1; i<= now ;i++){
if(s[i].score == s[i-1].score)
{
printf("%d",rank);
cnt++;
}else
{
rank = rank + cnt;
printf("%d",rank);
cnt = 1;
}
printf(" %s %d %d\n",s[i].school.c_str(),s[i].score,s[i].num);
}
return 0;
}