比赛的时候这个题目一直做不出来,WA了很多遍。比赛结束之后看别人的代码,发现竟然还需要排序!
经过我长久的思考,我终于发现了问题的所在,原来题目中的Lexicographicly是字典序的意思,这特么的!
排序之后,AC。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char name[15];
int score;
int id;
}map[10005];
int cmp(const void *a,const void *b)
{
if((*(node *)a).score!=(*(node *)b).score)
return (*(node *)a).score-(*(node *)b).score;
else
return strcmp((*(node *)a).name,(*(node *)b).name);
}
int main()
{
char NEW[15]={"NEW"},MAX[15]={"MAX"},AVE[15]={"AVERAGE"},DEL[15]={"DELETE"},QT[15]={"QUIT"};
int count;
char s[1005],s1[1005];
int i;
int temp,max;
int sum;
int t;
int flag;
count=0;
for(i=0;i<=1005;i++)
{
map[i].id=-1;
map[i].score=-1;
}
lab:
while(1)
{
scanf("%s",s);
if(strcmp(NEW,s)==0)
{
scanf("%s%d",s1,&temp);
for(i=0;i<count;i++)
{
if(map[i].id!=-1&&strcmp(map[i].name,s1)==0)
{
map[i].score=temp;
printf("update succeed\n");
getchar();
goto lab;
}
}
for(i=0;i<count;i++)
{
if(map[i].id==-1)
{
strcpy(map[i].name,s1);
map[i].id=1;
map[i].score=temp;
printf("A new record\n");
getchar();
goto lab;
}
}
strcpy(map[count].name,s1);
map[count].id=1;
map[count].score=temp;
count++;
getchar();
printf("A new record\n");
goto lab;
}
if(strcmp(s,MAX)==0)
{
max=-1;
if(count!=0)
qsort(map,count,sizeof(map[0]),cmp);
for(i=0;i<count;i++)
{
if(map[i].score>max&&map[i].id!=-1)
max=map[i].score;
}
if(max==-1)
{
printf("0 0\n");
getchar();
goto lab;
}
printf("%d ",max);
t=0;
for(i=0;i<count;i++)
{
if(map[i].score==max&&map[i].id!=-1)
t++;
}
printf("%d\n",t);
for(i=0;i<count;i++)
{
if(map[i].score==max&&map[i].id!=-1)
puts(map[i].name);
}
getchar();
goto lab;
}
if(strcmp(s,AVE)==0)
{
sum=0;
t=0;
for(i=0;i<count;i++)
{
if(map[i].id!=-1)
{
sum+=map[i].score;
t++;
}
}
if(t==0)
{
printf("0.00\n");
getchar();
goto lab;
}
else
{
printf("%.2f\n",sum*1.0/t);
getchar();
goto lab;
}
}
if(strcmp(s,DEL)==0)
{
flag=0;
scanf("%s",s1);
for(i=0;i<count;i++)
{
if(strcmp(s1,map[i].name)==0&&map[i].id!=-1)
{
map[i].id=-1;
flag=1;
}
}
if(flag==1)
printf("delete succeed\n");
else
printf("no such record\n");
getchar();
goto lab;
}
if(strcmp(s,QT)==0)
break;
}
return 0;
}