1025. PAT Ranking

http://pat.zju.edu.cn/contests/pat-a-practise/1025

灵活掌握容器的使用


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct stu
{
	string no;         
	int score;
	int location;      //学生所在位置
	int local;        //本地排名(注意并列的处理)
	int global;       //全局排名 
};

vector<stu> local_list;      //保存本地学生数据
vector<stu> global_list;     //保存全局学生数据


bool cmp(stu a,stu b)
{
  if (a.score!=b.score)       
  {
	  return a.score>b.score;
  }
  else
  {
	  return a.no<b.no;
  }
}

int main()
{
	//freopen("D:\\test.txt","r",stdin);
	int n,k,i,j;				//n个地区,每个地区k个学生数据
	cin>>n;
	for (i=1;i<=n;i++)
	{
		cin>>k;
        
		while (k--)                       //输入
		{
			stu s;
			cin>>s.no>>s.score;		     //输入编号,成绩
			s.location=i;				//位置就是i
			local_list.push_back(s);    
		}

		sort(local_list.begin(),local_list.end(),cmp);   //成绩排序

		for (j=0;j<local_list.size();j++)                //本地排名
		{     //表达式中有j-1,所以j!=0
			if (j!=0 && local_list[j].score==local_list[j-1].score ) 
			{
				local_list[j].local=local_list[j-1].local;
			}
			else
			{
				local_list[j].local=j+1;
			}
		}
		
		global_list.insert(global_list.end(),local_list.begin(),local_list.end()); //本地数据插进全局容器
		local_list.clear();                                                   //清空容器
	}
    cout<<global_list.size()<<endl;
	sort(global_list.begin(),global_list.end(),cmp);
    for (i=0;i<global_list.size();i++)                //全局排名
	{     
		if (i!=0 && global_list[i].score==global_list[i-1].score ) 
		{
			global_list[i].global=global_list[i-1].global;
		}
		else
		{
			global_list[i].global=i+1;
		}    
		cout<<global_list[i].no<<" "<<global_list[i].global<<" "
                    <<global_list[i].location<<" "<<global_list[i].local<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值