PAT_Ranking

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; 
struct STU{
	char id[14];
	int grade;
	int rank;
	int loc;
}stu[30000];

bool cmp(STU stu1,STU stu2){
	if(stu1.grade!=stu2.grade)
		return stu1.grade>stu2.grade;
	else{
		return strcmp(stu1.id,stu2.id)<0;//好好体会这句话 
	}
}

void _rank(int first,int last){
	stu[first++].rank = 1;
	int r=1;
	while(first<last){
		if(stu[first].grade < stu[first-1].grade){
			stu[first].rank = ++r;
		}
		else{
			stu[first].rank = stu[first-1].rank;
			r++;
		}
		first++;
	}
}

int main(){
	int N;
	scanf("%d",&N);
	int head = 0;
	int j = 0;		//一直记录数组位置 
	int total = 0;
	
	for(int i=0;i<N;i++){
		int k;
		scanf("%d",&k);
		for(int cal=0;cal<k;cal++){
			scanf("%s %d",stu[j].id,&stu[j].grade);
			stu[j].loc = i+1;
			j++;
			total++;
		}
		sort(stu+head,stu+j,cmp);
		_rank(head,j);
		head = j;
	}
	

	sort(stu+0,stu+total,cmp);
	cout<<total<<endl;
	int first =0 ,last = total;
	int r = 1;
	int temp = r;
	printf("%s %d %d %d\n",stu[0].id,r,stu[0].loc,stu[0].rank);
	first++;
	
	//计算排名
	//另一种方法:当前考生与上一名考生成绩不同时,他的排名等与当前遍历人数加1
	 
	while(first<last){
		if(stu[first].grade < stu[first-1].grade){
			printf("%s %d %d %d\n",stu[first].id,++r,stu[first].loc,stu[first].rank);
			temp = r;
		}
		else{
			printf("%s %d %d %d\n",stu[first].id,temp,stu[first].loc,stu[first].rank);
			r++;
		}
		first++;
	}
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值