1141 PAT Ranking of Institutions (25)

// PATA 20180318 B
Input Specification:
	Each input file contains one test case. 
	For each case, the first line gives a positive integer N (≤10^5), 
	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.

code ------------------------------------
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100000;
struct school {
    int Rank;
    char SchoolN[20];
    double TWS;
    int TWSInt;  //注意是按总分的int型来排名!
    int Ns;
} School[MAXN];
map<string,int> mapp;
int index1 = 0;
int findIndex(char SchoolName[20]) {
    if(mapp.find(SchoolName) != mapp.end()) {
        return mapp[SchoolName];
    } else {
        mapp[SchoolName] = index1;  ////
        return index1++;
bool cmp(school a, school b) {
    if(a.TWSInt != b.TWSInt) return a.TWSInt > b.TWSInt;
    if(a.Ns != b.Ns) return a.Ns < b.Ns;
    return strcmp(a.SchoolN,b.SchoolN) < 0;  ////
int main() {
    int N;
    char ID[20];
    double Score;
    char SchoolName[20];
    for(int i=0; i<N; i++) {
        scanf("%s %lf %s",&ID,&Score,&SchoolName);
        for(int j=0; j<strlen(SchoolName); j++)
            if(SchoolName[j] >= 'A' && SchoolName[j] <= 'Z') SchoolName[j] += 32;
        int currentIndex = findIndex(SchoolName);
        if(ID[0] == 'B') School[currentIndex].TWS += Score/1.5;
        if(ID[0] == 'T') School[currentIndex].TWS += Score*1.5;
        if(ID[0] == 'A') School[currentIndex].TWS += Score;
        School[currentIndex].Ns ++;
    for(int i=0; i<index1; i++)
        School[i].TWSInt = (int)School[i].TWS;
    sort(School, School + index1, cmp);
    School[0].Rank = 1;
    for(int i=1; i<index1; i++) {
        if(School[i].TWSInt == School[i-1].TWSInt) School[i].Rank = School[i-1].Rank;
        else School[i].Rank = i+1;
    for(int i=0; i<index1; i++)
        printf("%d %s %d %d\n",School[i].Rank,School[i].SchoolN,School[i].TWSInt,School[i].Ns);
    return 0;

大佬们说这是道水题... QAQ  QAQ  QAQ 




版权声明:转载请注明出处 https://blog.csdn.net/chushoufengli/article/details/80316439
想对作者说点什么? 我来说一句