去年现场赛遇到的题目,当时能力还不够,没有做这道题
现在能做了,在最后并列输出的时候有点坑,并且千万注意字符串排序时候的strcmp函数返回的并不是0 1(好像是>0 <0这一类的),也不能直接楞比较两个字符大小,必须用strcmp
解决了这个问题以后谜一样的就从15变25了
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct student
{
char name[50];
int score;
};
student s[10050];
int cmp(student a,student b)
{
if(a.score==b.score)
{
if(strcmp(a.name,b.name)>0)
{
return 0;
} //这里必须要使用strcmp 否则错
else
{
return 1;
}
}
else
{
return a.score>b.score;
}
}
int main(void)
{
int n,g,k;
scanf("%d%d%d",&n,&g,&k);
int total = 0;
for(int i=0;i<n;i++)
{
cin>>s[i].name>>s[i].score;
if(s[i].score>=60&&s[i].score<g)
{
total += 20;
}
else if(s[i].score>=g&&s[i].score<=100)
{
total += 50;
}
}
sort(s,s+n,cmp);
printf("%d\n",total);
int count = 0;
int paiming[1005];
for(int i=0;i<1005;i++)
{
paiming[i] = i+1;
}
int temp;
while(1)
{
printf("%d %s %d\n",paiming[count],s[count].name,s[count].score);
temp = count;
while(s[temp].score==s[temp+1].score)
{
paiming[temp+1] = paiming[temp];
temp++;
}
count++;
if(count>=k&&s[count].score!=s[count-1].score)
{
break;
}
}
}
/*
10 80 5
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 65
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 80
jack@ucla.edu 88
bob@cmu.edu 80
ken@163.com 70
*/
/*
10 80 7
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 78
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 87
jack@ucla.edu 88
bob@cmu.edu 79
ken@163.com 70
*/