大教训
| Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
|---|---|---|---|---|---|---|
| stdin/stdout | 3s | 16384K | 273 | 44 | Standard |
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;
}
本文介绍了一种用于计算学生平均分数并据此进行排名的方法。通过输入学生的姓名与分数,算法能够快速准确地给出每位学生的排名,并能处理同分情况下的排名问题。文中还提供了两种不同的实现代码供读者参考。
5604

被折叠的 条评论
为什么被折叠?



