PAT 甲级1055 The World‘s Richest (25分)

Note

  • 排序

  • “the output must be in non-decreasing alphabetical order of the names”

  • 字母顺序非降序排列(此时,不能用string直接排!!!要存放在char[]中,用strcmp)

  • 写cmp函数的时候 return strcmp(a.name, b.name) <= 0;

    return语句返回的是true或者false的值,所以要写<= 0这样的形式。比较ACSII码的大小,strcmp(‘a’, ‘z’)返回负值,因为a<z a – z < 0

  • 按照分数(int)的不降序排序,a.score <= b.score

  • 此外,学会看题目条件!!!N (人数) 有105个,要求输出的人最多只有100个,差距非常大。

    由此,可以先排序再从1~100岁里每个年龄段挑选出100个人 (按顺序加到vector数组中) ,这样再挑选输出,时间消耗会少很多。

Code:

#include<bits/stdc++.h>
using namespace std;

struct person{
	char name[10];
	int age,worth;
}p[100005];

bool compare(person a,person b){
	if(a.worth!=b.worth) return a.worth>b.worth;
	else if(a.age!=b.age) return a.age<b.age;
	else return (strcmp(a.name, b.name)<0);
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("data.txt","r",stdin);
	#endif
	
	int n,m,t;
	int max,amin,amax;
	int count[202]={0};
	vector<person> v;  //借鉴柳神Code优化 
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>p[i].name>>p[i].age>>p[i].worth;
	sort(p,p+n,compare);
	for(int i=0;i<n;i++){
		if(count[p[i].age]<100){
			v.push_back(p[i]);
			count[p[i].age]++;
		}
	} 
	
	for(int j=1;j<=m;j++){
		cin>>max>>amin>>amax;
		t=max;
		cout<<"Case #"<<j<<":"<<endl;
		for(int k=0;k<v.size();k++){
			if(v[k].age>=amin&&v[k].age<=amax){
				t--;
				cout<<v[k].name<<" "<<v[k].age<<" "<<v[k].worth<<endl;
			}
			if(t==0) break;
		}
		if(t==max) cout<<"None\n";
	}
	
	return 0;	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值