PAT(A) 1025

快排有问题,出现段错误,后来改用qsort过了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct pat Element;
struct pat{
	char id[15];
	int score;
	int local;
	int localrank;
	int totalrank;
} stu[30005];
int compare(struct pat a,struct pat b){
	if(a.score == b.score)
		return strcmp(a.id,b.id);
	else return b.score - a.score;
}
void swamp(Element *a,Element *b){
	Element tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}
Element median3(Element a[],int begin,int end){
	int mid;
	mid = (begin + end)/2;
	if(compare(a[begin],a[mid])>0)
		swamp(&a[begin],&a[mid]);
	if(compare(a[begin],a[end])>0)
		swamp(&a[begin],&a[end]);
	if(compare(a[mid],a[end])>0)
		swamp(&a[mid],&a[end]);
	swamp(&a[mid],&a[end-1]);
	return a[end-1];
}
void Insertsort(Element a[],int n){
	int i,j;
	Element tmp;
	for(i = 1;i<n;i++){
		tmp = a[i];
		for(j = i;compare(tmp,a[j-1])<0&&j>0;j--)
			a[j] = a[j-1];
		a[j] = tmp;
	}
}
void partition(Element a[],int begin,int end){
	int i,j;
	Element pivot;
	if(end - begin>4){
		pivot = median3(a,begin,end);
		i = 0;
		j = end-1;
		while(1){
			while(compare(a[++i],pivot)<0);
			while(compare(a[--j],pivot)>0);
			if(i<j)
				swamp(&a[i],&a[j]);
			else break;
		}
		swamp(&a[i],&a[end-1]);
		partition(a,begin,i-1);
		partition(a,i+1,end);
	}
	else Insertsort(a + begin,end - begin + 1);  //the array start from a + begin 
}
void quicksort(Element a[],int n){
	partition(a,0,n-1);
}
void merge(struct pat a[],int n,struct pat b[],int m){
	int ap,bp,tp;
	struct pat *tmp;
	tmp = (struct pat *)malloc(sizeof(struct pat)*(m+5));
	int i;
	for(i = 0;i<m;i++)
		tmp[i] = b[i];
	ap = bp = tp =  0;
	while(ap!=n&&tp!=m){
		if(compare(a[ap],tmp[tp])<0)
			b[bp++] = a[ap++];
		else b[bp++] = tmp[tp++];
	}
	while(ap!=n){
		b[bp++] = a[ap++];
	}
	while(tp!=m){
		b[bp++] = tmp[tp++];
	}
	free(tmp);
}
int main(){
	int n,k,total;
	struct pat *tmp;
	int i,j;
	FILE *fp;
	//fp = fopen("a.in","r");
	fscanf(stdin,"%d",&n);
	for(i = 0,total=0;i<n;i++){
		fscanf(stdin,"%d",&k);
		tmp = (struct pat*)malloc(sizeof(struct pat)*(k+5));
		for(j = 0;j<k;j++){
			fscanf(stdin,"%s%d",tmp[j].id,&tmp[j].score);
			tmp[j].local = i+1;
		}
		quicksort(tmp,k);
		tmp[0].localrank = 1;
		for(j = 1;j<k;j++)
			if(tmp[j].score == tmp[j-1].score)
				tmp[j].localrank = tmp[j-1].localrank;
			else tmp[j].localrank = j+1;
		merge(tmp,k,stu,total);
		total += k;
		free(tmp);
	}
	printf("%d\n",total);
	stu[0].totalrank = 1;
	for(i = 1;i<total;i++)
		if(stu[i].score == stu[i-1].score)
			stu[i].totalrank = stu[i-1].totalrank;
		else stu[i].totalrank = i+1;
	for(i = 0;i<total;i++)
		printf("%s %d %d %d\n",stu[i].id,stu[i].totalrank,stu[i].local,stu[i].localrank);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值