pat 1025 PAT Ranking

简单题,只是一开始用vector<student> rank[NUM]用得不太熟,巩固了一下sort的compare函数写法。得抓紧时间做题了,以后博客先略写,有空再来补充。


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
struct student
{
	string id;
	int score;
	int loc_id;
	int loc_rank;
};

// int cmp(const student &a,const student &b)
// {
// 	return a.score > b.score;
// }
bool operator<(student a,student b)
{
	if(a.score==b.score)
		return a.id < b.id;
	return a.score > b.score;
}

int cmp1(const void *a,const void *b)
{
	student *x=(student *)a;
	student *y=(student *)b;
	if(x->score==y->score)
		return x->score < y->score;
	return y->score - x->score;
}
int a[305];
student rankf[305][305];
vector<student> pat;
int main()
{
	int n,i,j,num;
	student s;
	//freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
	cin>>n;
	num=0;
	for(i=0;i<n;++i){
		cin>>a[i];
		for(j=0;j<a[i];++j){

			cin>>s.id;
			cin>>s.score;
			rankf[i][j]=s;
			num++;
		}

		//sort(rankf[i],rankf[i+1],cmp);//wrong
		//qsort(rankf[i],a[i],sizeof(rankf[i][0]),cmp1);//correct
		sort(rankf[i],rankf[i]+a[i]);//correct
	}
	

	for(i=0;i<n;++i){

		int index=0;
		for(j=0;j<a[i];++j){
			rankf[i][j].loc_id=i+1;

			
			if(rankf[i][j].score==rankf[i][index].score){
				rankf[i][j].loc_rank=index+1;
				
				
			} else{
				rankf[i][j].loc_rank=j+1;
				index=j;
			}

			pat.push_back(rankf[i][j]);
		}
	}
	sort(pat.begin(),pat.end());
	cout<<num<<endl;
	int final_rank,index=0;
	for(i=0;i<num;i++){
		if(pat[i].score==pat[index].score)
			final_rank=index+1;
		else {
			final_rank=i+1;
			index=i;
		}
		cout<<pat[i].id<<" "<<final_rank<<" "<<pat[i].loc_id<<" "<<pat[i].loc_rank<<endl;
		
	}

	return 0;
}



你可能感兴趣的:(pat 1025 PAT Ranking)