PAT A1062或者B1015 德才论

//ac了! 
//出现段错误的原因,1.字符串比较没有写<0;2.少一个=号 
//strcmp是有返回值的,前面大返回正数,后面大返回负数 
//使用algorithm中的sort要加using namespace std; 
//比较函数应该为满足本题需要而写。参数的顺序就应该是希望排列后的顺序 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100001
using namespace std; 
struct Student{
	char id[9];
	int de,cai,sum;//设置 一个总分,每次输入的时候求一下,然后就可以直接调用总分比较了,方便
	int flag;//表示考试类别:第1类到第5类 
}stu[N],temp;//数据比较大,不能用cin和cout了 
//分5类:
/*1.de、cai都>H 2.3.4.5依次向下*/ 
//写一个comp函数,这个我一开始也想到了,但是没想到结构体可以设置下类别,会更好比较
bool cmp(Student a,Student b){
	/*
	先按类别从小到大排序
	类别相同的,按总分从大到小排序
	总分相同的,按德分从大到小排序
	德分相同的,按准考证号从小到大排序 
	*/ 
	if(a.flag!=b.flag) return a.flag<b.flag;//小的在前面为true
	else if(a.sum!=b.sum) return a.sum>b.sum;//类别相同时,总分大的在前面 
	else if(a.de!=b.de) return a.de>b.de;
	else return strcmp(a.id,b.id)<0;//德分相同时,准考证小的在前面.这里的<0一开始居然忘记了! 
}
int main(){
	int n,L,H,num=0;
	scanf("%d %d %d",&n,&L,&H);
	while(n--){
		scanf("%s %d %d",temp.id,&temp.de,&temp.cai);
		if(temp.de>=L&&temp.cai>=L){
			temp.sum=temp.de+temp.cai;
			if(temp.de>=H&&temp.cai>=H) temp.flag=1;
			else if(temp.de>=H) temp.flag=2;
			else if(temp.de>=temp.cai) temp.flag=3;
			else temp.flag=4;	
			stu[num++]=temp;
			//temp=null;
		}
	}
	sort(stu,stu+num,cmp);
	printf("%d\n",num);
	for(int i=0;i<num;i++){
		printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
	}
	return 0;
} 

你可能感兴趣的:(C++,算法,pat)