PAT 1025 PAT Ranking

#include<iostream>

#include<string>

#include<queue>

#include<vector>

#include<cmath>

#include<iomanip>

#include<algorithm>

using namespace std;



struct Student

{

	string stu_id;

	int finRank;

	int locNum;

	int locRank;

	int score;

};





bool greaterMark(const Student &stu1, const Student &stu2)

{

	if(stu1.score != stu2.score)

		return stu1.score > stu2.score; //分数比较

	else

		return string(stu1.stu_id) < string(stu2.stu_id);

		//return strcmp(stu1.stu_id , stu2.stu_id)<0; //学号按非升序排列

}



bool cmp (const Student &stu1, const Student &stu2)

{

	return stu1.stu_id < stu2.stu_id;

}





void initRank(vector<Student> &vStu, bool isFinal)

{

	int rank = 1;

	for(int i=0; i<vStu.size(); i++)

	{

		//处理名次重复的问题

		if(i && vStu[i].score != vStu[i-1].score)

			rank = i+1;

		if(isFinal)

			vStu[i].finRank = rank;

		else

			vStu[i].locRank = rank;

	}

}



void initOneLocationStuInfo(vector<Student> &vTmpStu)

{

	sort(vTmpStu.begin(), vTmpStu.end(), greaterMark);

	initRank(vTmpStu,false);

}



//input

void input(vector<Student> &vAllStu)

{

	int i,j,k,n;

	cin>>n;

	Student stuTmp;	

	vector<Student> vTmpStu;

	for(i=1; i<=n; i++)

	{

		cin>>k;

		vTmpStu.clear();

		for(j=1; j<=k; j++)

		{

			cin>>stuTmp.stu_id;

			cin>>stuTmp.score;

			stuTmp.locNum = i;

			vTmpStu.push_back(stuTmp);

		}

		//init locNum and locRank

		initOneLocationStuInfo(vTmpStu);

		//将vTmpStu中的信息merge到vAllStu中。

		vAllStu.insert(vAllStu.end(), vTmpStu.begin(), vTmpStu.end() );

		

	}

}



void initFinalRank(vector<Student> &vAllStu)

{

	sort(vAllStu.begin(), vAllStu.end(), greaterMark);

	initRank(vAllStu,true);

}









void output(vector<Student> &vAllStu)

{

	cout<<vAllStu.size()<<endl;

	for(vector<Student>::iterator it=vAllStu.begin(); it!=vAllStu.end(); it++)

		cout<<it->stu_id<<" "<<it->finRank<<" "<<it->locNum<<" "<<it->locRank<<endl;

}



int main()

{

	int n,i;

	vector<Student> vAllStu;

	input(vAllStu);	

	initFinalRank(vAllStu);

	output(vAllStu);	

	return 0;

}

  

你可能感兴趣的:(rank)