Java 1085 PAT单位排行

题目内容:

每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。

输入格式:

输入第一行给出一个正整数 N(≤105),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:

准考证号 得分 学校

其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。

输出格式:

首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

排名 学校 加权总分 考生人数

其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5整数部分考生人数是该属于单位的考生的总人数。

学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。

输入样例:

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

结尾无空行

输出样例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

结尾无空行

Java代码实现(未AC,最后2个测试点未通过):

import java.io.*;
import java.util.*;
 
public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		Map<String,School1085> map = new HashMap<String,School1085>();
		for(int i = 0; i < n;i++) {
			String[] split = br.readLine().split(" ");
			char type = split[0].charAt(0);
			double score = Integer.parseInt(split[1]);
			if(type == 'B') {
				score = score / 1.5;
			}else if(type == 'T') {
				score = score * 1.5;
			}
			String name = split[2].toLowerCase();
			School1085 value = map.get(name);
			if(value == null) {
				School1085 school = new School1085(name, score);
				map.put(name, school);
			}else {
				value.count += 1;
				value.sum += score;
			}
		}
		Set<String> keySet = map.keySet();
		Iterator<String> iterator = keySet.iterator();
		List<School1085> list = new ArrayList<School1085>();
		while(iterator.hasNext()) {
			String key = iterator.next();
			School1085 school1085 = map.get(key);
			list.add(school1085);
		}
		Collections.sort(list);
		
		list.get(0).rank = 1;
		for(int i = 1; i < list.size();i++) {
			if(list.get(i).sumEnd == list.get(i - 1).sumEnd) {
				list.get(i).rank = list.get(i - 1).rank;
			}else {
				list.get(i).rank = i + 1;
			}
		}
        
        System.out.println(list.size());
		for(School1085 data : list) {
			System.out.println(data);
		}
	}
}
class School1085 implements Comparable<School1085>{
	String name;
	int count = 1;
	int sum;
	int rank;
	
	public School1085(String name, double sum) {
		this.name = name;
		this.sum = (int)sum;
	}
 
	@Override
	public int compareTo(School1085 o) {
		if(this.sum != o.sum) {
			return -(this.sum - o.sum);
		}else if(this.count != o.count) {
			return this.count - o.count;
		}else {
			return this.name.compareTo(o.name);
		}
		
	}
 
	@Override
	public String toString() {
		return rank + " " + name + " " + sum + " " + count;
	}
}

C++代码实现(AC):

#include <iostream>
#include <algorithm>
#include<cstdio>
#include <cctype>
#include <vector>
#include <map>
#include<bits/stdc++.h>
using namespace std;
struct node{
    string school;
    int sum;
    int num;
};
bool cmp(node a,node b)
{
    if(a.sum!=b.sum)
        return a.sum>b.sum;
    else if(a.num!=b.num)
        return a.num<b.num;
    else
        return a.school<b.school;
}
int main(){
    int N;
    cin>>N;
    map<string,double> sc;//学校成绩
    map<string,int> sn;//学校人数
    for(int i=0;i<N;i++)
    {
        string no;//准考证号
        double score;//成绩
        string sch;//学校
        cin>>no;
        cin>>score>>sch;
        for(int j=0;j<sch.length();j++)
        {
            sch[j]=tolower(sch[j]);
        }
 
        if(no[0]=='B')
        {
            sc[sch]+=score/1.5;
        }
        if(no[0]=='A')
        {
            sc[sch]+=score;
        }
        if(no[0]=='T')
        {
            sc[sch]+=score*1.5;
        }
        sn[sch]++;
    }
     vector<node>ans;
     map<string,double>::iterator it;
     for(it = sc.begin();it!=sc.end();it++)
     {
         ans.push_back(node{it->first,sc[it->first],sn[it->first]});
     }
     sort(ans.begin(),ans.end(),cmp);
     printf("%d\n",ans.size());int k=-1,tmp=0;
     for(int i=0;i<ans.size();i++)
     {
         if(k!=ans[i].sum)tmp=i+1;
         k=ans[i].sum;
         printf("%d ",tmp);
         cout<<ans[i].school;
         printf(" %d %d\n",ans[i].sum,ans[i].num);
     }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值