A1025 patranking和算法筆記的思路不同

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
struct student{
	char name[15];
	int s,kc,r,local;
}stu[30010];//定義所有學生結構體!一定要注意這裏的取值!
//題目規定不超過100個考場!每個考場不超過300個人! 即該值不小於100×300=30000人!! 

bool cmp(student a,student b){
	if(a.s!=b.s) return a.s>b.s;
	else return strcmp(a.name,b.name)<0;  
}//定義比較函數 

int main(){
	int i,j,k=0,n,a,b=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d",&a);
		b+=a;
		for(j=0;j<a;j++){
			++k;
			scanf("%s %d",&stu[k].name,&stu[k].s);
			stu[k].kc=i;//記錄考場! 
		}
	}//分考場輸入所有學生分數,並且記錄考場!
	printf("%d\n",b); 
	
	sort(stu+1,stu+k+1,cmp);//全局排序!! 
	/*for(i=1;i<=k;i++){
		printf("%s %d %d %d\n",stu[i].name,stu[i].r,stu[i].kc,stu[i].local);
	}//(用來測試!)全局輸出!!! */
	
	for(i=1;i<=k;i++){
		if(i!=1&&stu[i].s==stu[i-1].s) stu[i].r=stu[i-1].r;
		else stu[i].r=i;
	}//全局排名!!!!!! 
	
	int now=0,bef=0;
	for(i=0;i<n;i++){//n個教室 
		for(j=1;j<=k;j++){
			if(stu[j].kc==i+1){//是該考場則記錄!
				 now++;
				if(j!=1&&stu[j].s==stu[bef].s)
				stu[j].local=stu[bef].local;
				else stu[j].local=now;
				bef=j;
			}
	
		}
		now=0;
	}//分教室排名!!!!! 
	
	for(i=1;i<=k;i++){
		printf("%s %d %d %d\n",stu[i].name,stu[i].r,stu[i].kc,stu[i].local);
	}//全局輸出!!! 
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章