PAT Basic Level 1085 PAT单位排行 (25 分)

题目链接:

https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400

我的代码(22分,无法修改了,用map排的序,):

#include <iostream>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cstring>
using namespace std;

const int maxn=100010;
struct SCHOOL{
    char number[6];
    int score;
    char school[6];
}sch[maxn];
/*
bool cmp(map<string,double>::iterator a,map<string,double>::iterator b){
    return a->second > b->second;
}
*/
//学校名称,总分存储
map<string,double> m;
map<string,int> amount;//存储学校人数

int a[maxn];//用于输出排名
int main(){
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        char school[6];
        scanf("%s %d %s",sch[i].number,&sch[i].score,sch[i].school);
        for(int j=0;j<strlen(sch[i].school);j++)
            if ((sch[i].school[j] >= 'A') && (sch[i].school[j] <= 'Z'))
                sch[i].school[j]+='a'-'A';
        amount[sch[i].school]++;
    }
    for(int i=0;i<N;i++){
        if(m[sch[i].school]==0)
            m[sch[i].school]=0;
        if(sch[i].number[0]=='A')
            m[sch[i].school]+=sch[i].score*1.0;
        else if(sch[i].number[0]=='B')
            m[sch[i].school]+=sch[i].score*1.0/1.5;
        else if(sch[i].number[0]=='T')
            m[sch[i].school]+=sch[i].score*1.0*1.5;
    }
//    multimap<double,string,greater<double> >_cnt;
    multimap<int,string,greater<int> >_cnt;
    int num=1;
    for(map<string,double>::iterator itr = m.begin();itr!=m.end();itr++)
    {

        _cnt.insert(pair<int,string>(itr->second,itr->first));
    }

    for(multimap<int,string,greater<int> >::iterator it=_cnt.begin();it!=_cnt.end();it++){
        int tmp=it->first;
        a[num++]=tmp;
    }
    int rank_=1;
    int i=1;
    cout<<_cnt.size()<<endl;
    for(multimap<int,string,greater<int> >::iterator it=_cnt.begin();it!=_cnt.end();it++){
    //问题:
    //在对成绩排序的同时,无法对学校人数排序,
    //用multimap不恰当,测试点5根本就无法修改,所以是22分
        int score=it->first;
        if(i==1){
            printf("1 ");
        }
        else if(a[i]==a[i-1]){
            printf("%d ",rank_);
        }
        else if(a[i]!=a[i-1]){
            rank_=i;
            printf("%d ",i);
        }
        i++;
//        cout<<it->first<<" "<<it->second<<endl;
        cout<<it->second<<" "<<score;

        cout<<" "<<amount[it->second]<<endl;
    }
    return 0;
}

AC代码(用map引入school,score,人数,然后利用vector<node> ans,结点来排序):

#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
struct node {
	string school;
	int tws, ns;//tws:代表该学校总分,ns:代表该学校的学生
};

bool cmp(node a, node b) {
	if (a.tws != b.tws)
		return a.tws > b.tws;
	else if (a.ns != b.ns)
		return a.ns < b.ns;
	else
		return a.school < b.school;
}

int main() {
	int n;
	scanf("%d", &n);
	unordered_map<string, int> cnt;
	unordered_map<string, double> sum;
	//unordered_map<string,int> sum;我写成了 
	for (int i = 0; i < n; i++)
	{
		string id, school;
		cin >> id;
		double score;
		scanf("%lf", &score);
		cin >> school;
		for (int j = 0; j < school.length(); j++) {
			school[j] = tolower(school[j]);
		}
		if (id[0] == 'B')
			score = score / 1.5;
		else if (id[0] == 'T')
			score = score * 1.5;
		sum[school] += score;
		cnt[school]++;
	}
	vector<node> ans;

	for (auto it = cnt.begin(); it != cnt.end(); it++) {
		ans.push_back(node{ it->first,(int)sum[it->first],cnt[it->first] });
	}
	sort(ans.begin(), ans.end(), cmp);
	int len = ans.size();
	int rank = 0, pres = -1;
	printf("%d\n", len);
	for (int i = 0; i < len; i++) {
		if (pres != ans[i].tws)rank = i + 1;
		pres = ans[i].tws;
		printf("%d ", rank);
		cout << ans[i].school;
		printf(" %d %d\n", ans[i].tws, ans[i].ns);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值