前言(碎碎念):
首先,强调一点,本题不涉及算法,一个比较麻烦的基础题而已,学算法的朋友可以去看其他ti了。
刚考了乙级基础题,前几题还挺简单,最后一题有点“小”麻烦。
踩的坑如下:
1.本来打算用cin读入各奖牌数量的,发现不太行,最后一个字符一个字符读的(哭死)
2.结构体不是用的a,b,c三个字母吗,我定义字符的时候下意识也用c。所以最后把结构体里的铜牌表示为d了,应该能看(心虚)
3.最后有一个测试用例过不了,我以为是因为没加ios::sync_with_stdio等等一串,发现加了后,测试样例全答案错误了。(有点难评,其实我现在还不知道为什么)
4.最后才想起来,如果第一个得奖数全为0,那不就是和第0个相等了,最后的一位的排名被赋值为了0(笑不活了,排第0名)
问题描述:
输入输出样例:
规模限制:
自定义比较函数代码:
bool cmp(const student&stu1,const student&stu2)
{
if(stu1.a!=stu2.a) return stu1.a>stu2.a;
else
{
if(stu1.b!=stu2.b) return stu1.b>stu2.b;
else
{
if(stu1.d!=stu2.d) return stu1.d>stu2.d;
else return 0;
}
}
}
思路概述:
(1)读入数据
(2)排序
(3)输出数据
满分代码:
#include<bits/stdc++.h>
using namespace std;
struct student
{
int a=0,b=0,d=0;
char id[12];
int ans;
}stu[11234];
bool cmp(const student&stu1,const student&stu2)
{
if(stu1.a!=stu2.a) return stu1.a>stu2.a;
else
{
if(stu1.b!=stu2.b) return stu1.b>stu2.b;
else
{
if(stu1.d!=stu2.d) return stu1.d>stu2.d;
else return 0;
}
}
}
int main()
{
int n;
char c;
cin>>n;
getchar();
for(int i=1;i<=n;i++)
{
getchar();
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].a=stu[i].a*10+(int)(c-'0');
c=getchar();
}
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].b=stu[i].b*10+(int)(c-'0');
c=getchar();
}
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].d=stu[i].d*10+(int)(c-'0');
c=getchar();
}
cin>>stu[i].id;
getchar();
}
sort(stu+1,stu+1+n,cmp);
for(int i=1;i<=n;i++)
{
stu[i].ans=i;
if((i!=1)&&(stu[i].d==stu[i-1].d&&stu[i].a==stu[i-1].a&&stu[i].b==stu[i-1].b))
{
stu[i].ans=stu[i-1].ans;
}
}
int m;
cin>>m;
char z[12];
int x;
for(int i=1;i<=m;i++)
{
cin>>z;
for(int j=1;j<=n;j++)
{
if(strcmp(stu[j].id,z)==0)
{
x=j;
break;
}
}
cout<<stu[x].id<<" "<<stu[x].ans<<" "<<"G"<<stu[x].a<<"S"<<stu[x].b<<"B"<<stu[x].d<<"\n";
}
return 0;
}
代码讲解:
#include<bits/stdc++.h>
using namespace std;
//定义结构体
struct student
{
int a=0,b=0,d=0;
char id[12];
int ans;
}stu[11234];
//自定义比较函数
bool cmp(const student&stu1,const student&stu2)
{
if(stu1.a!=stu2.a) return stu1.a>stu2.a;
else
{
if(stu1.b!=stu2.b) return stu1.b>stu2.b;
else
{
if(stu1.d!=stu2.d) return stu1.d>stu2.d;
else return 0;
}
}
}
int main()
{
//数据读入结构体
int n;
char c;
cin>>n;
getchar();
for(int i=1;i<=n;i++)
{
getchar();
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].a=stu[i].a*10+(int)(c-'0');
c=getchar();
}
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].b=stu[i].b*10+(int)(c-'0');
c=getchar();
}
c=getchar();
while(c>='0'&&c<='9')
{
stu[i].d=stu[i].d*10+(int)(c-'0');
c=getchar();
}
cin>>stu[i].id;
getchar();
}
//sort函数排序
sort(stu+1,stu+1+n,cmp);
//将排序后的排名读入结构体
for(int i=1;i<=n;i++)
{
stu[i].ans=i;
if((i!=1)&&(stu[i].d==stu[i-1].d&&stu[i].a==stu[i-1].a&&stu[i].b==stu[i-1].b))
{
stu[i].ans=stu[i-1].ans;
}
}
//读入要查询的编号,并进行查询
int m;
cin>>m;
char z[12];
int x;
for(int i=1;i<=m;i++)
{
cin>>z;
for(int j=1;j<=n;j++)
{
if(strcmp(stu[j].id,z)==0)
{
x=j;
break;
}
}
cout<<stu[x].id<<" "<<stu[x].ans<<" "<<"G"<<stu[x].a<<"S"<<stu[x].b<<"B"<<stu[x].d<<"\n";
}
return 0;
}