天天看點

PAT Basic Level 1085 PAT機關排行 (25 分)

題目連結:

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

22分代碼(測試點5沒通過,網上說是:輸出分數是整數,我的也時輸出整數,但是沒通過,還望大佬訂正):

#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;
    int num=1;
    for(map<string,double>::iterator itr = m.begin();itr!=m.end();itr++)
    {
        _cnt.insert(pair<double,string>(itr->second,itr->first));
    }

    for(multimap<double,string,greater<double> >::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<double,string,greater<double> >::iterator it=_cnt.begin();it!=_cnt.end();it++){
        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;
}
           

參考的滿分代碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <map>
#include <string>
#include <unordered_map>
using namespace std;

const int maxn = 100010;
struct SCHool {
	string name;//插入存在一一對應的關系
	int toSco;
	int peo;
}sch[maxn];

bool cmp(struct SCHool a, struct SCHool b) {
	if (a.toSco != b.toSco)
		return a.toSco > b.toSco;
	else if (a.peo != b.peo)
		return a.peo < b.peo;
	else
		return a.name < b.name;
}
unordered_map<string, double> sum;
unordered_map<string, int> cnt;

int main() {
	int n;
	scanf("%d", &n);
	string tmp_name, tmp_sch;
	double tmp_scr;
	for (int i = 0; i < n; i++) {
		cin >> tmp_name;
		scanf("%lf", &tmp_scr);
		cin >> tmp_sch;
		for (int j = 0; j < tmp_sch.size(); j++) {
			tmp_sch[j] = tolower(tmp_sch[j]);
		}
		if (tmp_name[0] == 'B')
			tmp_scr /= 1.5;
		else if (tmp_name[0] == 'T')
			tmp_scr *= 1.5;
		sum[tmp_sch] += tmp_scr;
		cnt[tmp_sch]++;
	}
	vector<SCHool> ans;

	for (auto it = cnt.begin(); it != cnt.end(); it++) {
		ans.push_back(SCHool{ it->first,(int)sum[it->first],cnt[it->first] });
	}
	sort(ans.begin(), ans.end(), cmp);
	int len = ans.size();
	int rank = 1;

  printf("%d\n",len);
	printf("1 ");
	cout << ans[0].name;
	printf(" %d %d\n", ans[0].toSco, ans[0].peo);
	for (int i = 1; i < len; i++) {
		if (ans[i].toSco != ans[i-1].toSco) {
			rank = i + 1;
			printf("%d ", rank);
		}
		else {
			printf("%d ", rank);
		}
		cout << ans[i].name;
		printf(" %d %d\n", ans[i].toSco, ans[i].peo);
	}

	return 0;
}