A1025

A1025(PAT)

题目描述

PAT Ranking

代码

#include<stdio.h>
#include<string.h>
#include<algorithm> 
using namespace std;

struct student{
	char id[14];
	int score;
	int team_number;
	int local_rank;
	int all_rank;
}stu[1000000];

bool cmp(student a,student b)
{
	if(a.score!=b.score)
	{
		return a.score>b.score;
	}
	else
	{
		return strcmp(a.id,b.id)<0;
	}
	
}

int main()
{
	int total=0;	//总人数 
	int num=0;	//num为队伍数
	scanf("%d",&num);
	for(int i=1;i<=num;i++) 
	{
		int n=0;	//n为该队伍中的队员数 
		scanf("%d",&n);
		for(int j=1;j<=n;j++)
		{
			scanf("%s %d",stu[total].id,&stu[total].score);
			stu[total].team_number=i;
			total++;	
		}
		
		//在队伍内进行排序
		sort(stu+total-n,stu+total,cmp);
		stu[total-n].local_rank=1;
		for(int j=total-n+1;j<total;j++)
		{
			if(stu[j].score==stu[j-1].score)
			{
				stu[j].local_rank=stu[j-1].local_rank;
			}
			else
			{
				stu[j].local_rank=j-(total-n+1)+1+1;
			}
		}
		
	}
	
	//在队伍外进行排序
	sort(stu,stu+total-1,cmp);

	printf("%d\n",total);
	for(int i=0;i<total;i++) 
	{
		if(i==0)
		{
			stu[i].all_rank=1;
		}
		else if(stu[i].score==stu[i-1].score)
		{
			stu[i].all_rank=stu[i-1].all_rank;
		}
		else
		{
			stu[i].all_rank=i+1;
		}
		printf("%s %d %d %d\n",stu[i].id,stu[i].all_rank,stu[i].team_number,stu[i].local_rank);
	}
	
	
	return 0;
}

思路

数据输入后先在组内排序,再对整体进行排序。构造student结构体对输入信息进行存储。

收获

  1. 注意c++标准库中 sort(start, end, 排序方法) 函数的的用法[1]
  2. 利用cmp()函数的返回,巧妙实现先排序成绩,成绩相同时再排序编号。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值