pat每日刷题计划--day65

PAT A1025  PAT Ranking

给定一组id和对应的得分及考场,给他们排序,要本场排名和最终排名,并按照要求格式输出结果

题目链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872

几点注意事项

  • 使用sort(first address,final address+1,cmp)里面是要的地址,所以对于数组a,a[0]-a[3],应该是sort(a,a+4,cmp)。不是a[0]。另外是左闭右开
  • strcmp(a.id,b.id)<0指的是a的字典序小于b的时候返回小于零的数字(并不一定是-1)
  • cmp在自定义的时候,遇到a.score!=b.score都是返回a.score<b.score,不能写在balabala返回true哦
  • 注意一下循环嵌套对应的不要同一个字母,还有就是有的时候是j+1,看清就行,没难度
  • 排名得出方法:如果相同,则为前一个的排名,如果不同,就是在本组的下标+1
  • 结构组定义char id[20]是可以的,读取直接%s %d,按照空格就ok
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct student
{
    char id[20];//个人id号
    int localnum;//属于哪个地方
    int score;//个人得分
    int finalrank=1;//最后排名
    int localrank=1;
}stu[30005];
bool cmp(student stu1,student stu2)
{
    if(stu1.score!=stu2.score)
        return stu1.score>stu2.score;
    return strcmp(stu1.id,stu2.id)<0;
}
int main()
{
    int n,k;
    int num=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&k);
        for(int j=1;j<=k;j++)
        {
            scanf("%s %d",stu[num].id,&stu[num].score);
            stu[num++].localnum=i;
        }
        sort(stu+num-k,stu+num,cmp);//num-k,num-1
        for(int j=1;j<=k-1;j++)
        {
            if(stu[j+num-k].score!=stu[j+num-k-1].score)
                stu[j+num-k].localrank=j+1;
            else
                stu[j+num-k].localrank=stu[j+num-k-1].localrank;
        }
    }
    sort(stu,stu+num,cmp);
    printf("%d\n",num);
    printf("%s %d %d %d\n",stu[0].id,stu[0].finalrank,stu[0].localnum,stu[0].localrank);
    for(int i=1;i<num;i++)
    {
        if(stu[i].score==stu[i-1].score)
            stu[i].finalrank=stu[i-1].finalrank;
        else
            stu[i].finalrank=i+1;
        printf("%s %d %d %d\n",stu[i].id,stu[i].finalrank,stu[i].localnum,stu[i].localrank);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/tingxilin/p/12207583.html