1153 Decode Registration Card of PAT (25 分) 用printf进行输出

这道题的输出一定要用printf不能用cout,否则最后一个测试点会多接近100ms的运行时间。 

#include<cstdio>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
	int site;
	int num;
};
struct student{
	string id;
	int score;
	char level;
	int site;
	int date;
}stu[10000]; 
int getInt(string s, int lend, int rend)
{
	string tmp;
	for(int i=lend; i<=rend; i++){
		tmp.push_back(s[i]);
	}
	return stoi(tmp);
}
bool cmp1(student a,student b)
{
	if(a.score!=b.score) return a.score>b.score;
	else return a.id<b.id;
}
bool cmp2(node a, node b){
	if(a.num!=b.num)return a.num>b.num;
	else return a.site<b.site;
}
int main()
{
	int n,m,score,type;
	string id;
	scanf("%d%d",&n,&m);
	for(int i=0; i<n; i++){
		cin>>id>>score;
		stu[i].id = id;
		stu[i].score = score;
		stu[i].level = id[0];
		stu[i].site = getInt(id,1,3);
		stu[i].date = getInt(id,4,9);
	}
	char term1;
	int term2, term3;
	for(int i=0; i<m; i++){
		scanf("%d",&type); getchar();
		if(type==1){
			scanf("%c",&term1);
			printf("Case %d: 1 %c\n",i+1,term1);
			vector<student> v;
			for(int j=0; j<n; j++){
				if(stu[j].level==term1){
					v.push_back(stu[j]);
				}
			}
			sort(v.begin(),v.end(),cmp1);
			for(int j=0; j<v.size(); j++){
				printf("%s %d\n",v[j].id.c_str(),v[j].score);//这一句绝对不能用cout,否则最后一个测试点通过的几率很低。用cout的时间会多100ms左右
				//cout<<v[j].id<<" "<<v[j].score<<endl;
			}
			if(v.size()==0) printf("NA\n");
		}
		else if(type==2){
			scanf("%3d",&term2);
			printf("Case %d: 2 %d\n",i+1,term2);
			int cnt=0,total=0;
			for(int j=0; j<n; j++){
				if(stu[j].site==term2){
					cnt++;
					total+=stu[j].score;
				}
			}
			if(cnt==0) printf("NA\n");
			else printf("%d %d\n",cnt,total);
		}
		else if(type==3){
			scanf("%d",&term3);
			printf("Case %d: 3 %06d\n",i+1,term3);
			map<int,int> mp;
			for(int j=0; j<n; j++){
				if(stu[j].date==term3){
					if(mp.count(stu[j].site)==0){
						mp[stu[j].site]=1;
					}
					else{
						mp[stu[j].site]++;
					}
				}
			}
			vector<node> vi;
			for(auto it=mp.begin();it!=mp.end();it++){
				node tmp;
				tmp.site = it->first;
				tmp.num = it->second;
				vi.push_back(tmp);
			}
			sort(vi.begin(),vi.end(),cmp2);
			for(int j=0; j<vi.size(); j++){
				printf("%03d %d\n",vi[j].site,vi[j].num);
			} 
			if(vi.size()==0) printf("NA\n");
		}
	}
	return 0;
}







 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值