JOJ 2319: Score List

本文介绍了一种用于计算学生平均分数并据此进行排名的方法。通过输入学生的姓名与分数,算法能够快速准确地给出每位学生的排名,并能处理同分情况下的排名问题。文中还提供了两种不同的实现代码供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大教训


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout3s16384K27344Standard

Student's ranklist is sorted by their average score. Given the list of name and score, you can point out everyone's rank.

Input

There are several lines in the input, the first line consist of two integer N, M (1<=N,M<=1000), In the next N lines, an integer x is followed by a string whose score is x, the length of name is no more than 10 character.The next M lines are questions, each line is an integer y.

Output

For each case, you should calculate the ranklist. For each question, you should print the name whose rank is y. If there are more than one person, print their names alphabetically separated by a space. If the rank y is invalid, print "Invalid".

Notice there are paratactic persons whose average score are the same. The next rank following rank n is always n+1, in spite of how many persons are in rank n.

Sample Input

5 4
Tom 100
Mary 70
Mary 90
Bob 80
Alice 75
1
2
3
4

Sample Output

Tom
Bob Mary
Alice
Invalid

 

Problem Source: skywind

 

 

 

 

孔牛的代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct t
{
    double score;
    char name[11];
    int num;
}a[1005];
bool equal(double e,double t)
{
    if((fabs(e-t))<1e-6) return true;
    return false;
}
bool cmp(t p,t q)
{
    if(equal(p.score,q.score)) return strcmp(p.name,q.name)<0;
    return p.score>q.score;
}
int main()
{
    freopen("in.txt","r",stdin);
 freopen("out.txt","w",stdout);
    int n,kk,i,j;
    while(scanf("%d%d",&n,&kk)==2)
    {
        int l=0;
        char str[11];
        int  x;
        for(i=0;i<1005;i++) a[i].num=1;
        for(i=0;i<n;i++)
        {
            scanf("%s%d",str,&x);
            bool flag=true;
            for(j=0;j<l;j++)
            if(strcmp(a[j].name,str)==0)
   {
     a[j].score+=x;
     a[j].num++;
     flag=false;
   }
            if(flag)
            {
                strcpy(a[l].name,str);
                a[l++].score=x;
            }
        }
        for(i=0;i<l;i++) a[i].score/=a[i].num;
        sort(a,a+l,cmp);
        int ll=1;
        for(i=1;i<l;i++)
  if(!equal(a[i].score,a[i-1].score))
                               ll++;
        while(kk--)
        {
            int rank;
            scanf("%d",&rank);
            if(rank>=ll+1||rank<=0)
   {
    printf("Invalid/n");
    continue;
   }
            if(rank==1)
            {
                printf("%s",a[0].name);
                i=1;
                while(i<l&&equal(a[i].score,a[i-1].score))
                {
                    printf(" %s",a[i].name);
                    i++;
                }
                printf("/n");
                continue;
            }
           int k=1,i=1;
           while(k<rank)
           {
                if(!equal(a[i].score,a[i-1].score))
                {
                     k++;
                     if(k==rank) break;
                }
                i++;
           }
          printf("%s",a[i].name);
          i++;
          while(i<l&&equal(a[i].score,a[i-1].score))
          {
              printf(" %s",a[i].name);
              i++;
          }
          printf("/n");
        }
    }
    return 0;
}

 

 

我的代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct t
{
   char s[100];
   double score;
   int num,mingci;//别忘了初始化
}human[1005];
int n,m,i,k,y,j;
char str[100];
bool cmp(struct t a,struct t b)
{
 return strcmp(a.s,b.s)<0;//就是这里错了
}
int main()
{
 freopen("in.txt","r",stdin);
 freopen("out.txt","w",stdout);
 while(scanf("%d%d",&n,&m)!=EOF)
 {
  int count1=0;
  for(i=0;i<n;i++)
      human[i].s[0]='/0';
  for(i=0;i<n;i++)
  {
   scanf("%s%d",str,&k);
   bool flag=true;
   for(j=0;j<count1;j++)
   if(strcmp(human[j].s,str)==0)
   {
    human[j].score+=k;
    human[j].num++;
    flag=false;
   }
   if(flag)
   {
    human[count1].score=k;
    human[count1].num=1;
    strcpy(human[count1].s,str);
    human[count1].mingci=0;
    count1++;
   }
  }
  sort(human,human+count1,cmp);
        for(i=0;i<count1;i++)
   human[i].score=human[i].score/human[i].num;
  int num2=0,num3=0;
        while(num3<count1)
        {
      num2++;
            double max=-1e15;
      for(j=0;j<count1;j++)
      if(human[j].score>max&&human[j].mingci==0)
          max=human[j].score;
      for(j=0;j<count1;j++)
      if(human[j].score==max&&human[j].mingci==0)
      {
    human[j].mingci=num2;
    num3++;
      }
  }
  int kk;
  for(i=1;i<=m;i++)
  {
   scanf("%d",&kk);//怎么按名字顺序书出来?
   bool flag=false;
   int tt=0;
   for(j=0;j<count1;j++)
   if(human[j].mingci==kk)
   {
    if(tt) printf(" ");
    printf("%s",human[j].s);
    flag=true;
    tt=1;
   }
   if(!flag) printf("Invalid");
   printf("/n");
  }
 }
 return 0;
}


This problem is used for contest: 51  153 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值