C++版浙大PAT乙级1095(25分)

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

主要是情况3容易超时,把cout改成printf效率会高一些。 

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

struct stu{// 学生
	string num;
	int score;
};

struct kao{// 考场
	int num=0;
	int student=0;
};

bool cmp1(stu a, stu b){
	if(a.score!=b.score)
		return a.score > b.score;
	else
		return a.num < b.num;
}

bool cmp2(kao a, kao b){
	if(a.student!=b.student)
		return a.student > b.student;
	else
		return a.num < b.num;
}

int main() {
	string str;
	int n, m, t, flag;
	cin >> n >> m;
	stu stus[n+1];
	for(int i=1; i<=n; i++){
		cin >> stus[i].num >> stus[i].score;
	}
	for(int i=0; i<m; i++){
		cin >> t >> str;
		printf("Case %d: %d %s\n", i+1, t, str.c_str());
		flag = 0;
		// 查开头
		if(t==1){
			int k=1;
			stu rank[n+1];
			for(int j=1; j<=n; j++){
				if(stus[j].num[0]==str[0]){
					rank[k].num = stus[j].num;  
					rank[k++].score = stus[j].score;
				}
			}
			sort(rank+1, rank+k, cmp1);
			for(int j=1; j<k; j++){
				flag = t;
				printf("%s %d\n", rank[j].num.c_str(), rank[j].score);
			}
		// 查考场人数和总分数
		}else if(t==2){
			int count=0, total=0;
			for(int j=1; j<=n; j++){
				if(stus[j].num.substr(1, 3)==str){
					count ++;
					total += stus[j].score;
				}
			}
			if(count!=0){
				printf("%d %d\n", count, total);
				flag = t;
			}
		// 查指定日期的每个考场的考场号和人数
		}else if(t==3){
			int temp, l=0, max=-1;
			kao kaos[1000];
			for(int j=1; j<=n; j++){
				if(stus[j].num.substr(4, 6)==str){
					// 考场号101~999
					temp = atoi(stus[j].num.substr(1, 3).c_str());
					if(kaos[temp-101].num==0){
						l ++;
						kaos[temp-101].num = temp;
						if(max<temp-101)
							max = temp-101;
					}
					kaos[temp-101].student ++;
				}
			}
			sort(kaos, kaos+max+1, cmp2);
			for(int j=0; j<l; j++){
				printf("%d %d\n", kaos[j].num, kaos[j].student);
				flag = t;
			}
		}
		if(flag == 0){
			cout << "NA" << endl;
		}
	}
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值