B3968 [GESP202403 五级] 成绩排序
本人的代码风格可能有点乱(肮脏)
代码里的注释还算详细,就是变量名有点抽象
/*
B3968
[GESP202403 五级] 成绩排序
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=10005;
struct node
{
int chinese;//语文
int math;//数学
int english;//英语
int sum;//三科总分
int chinese_and_math_sum;//语文和数学的总分
int yuanlaidebianhao;//原来的编号
int xianzaidepaiming;//现在的排名
int chinese_and_math_max;//语文和数学的最大值
bool b;//一个标记变量
};
node a[maxn];
//bool cmp(node,node);
bool cmp(node x,node y)//排序函数1,用于根据成绩排名,但是会打乱原来数组的顺序
{
if(x.sum>y.sum)//总分不同直接排
{
return x.sum>y.sum;
}
if(x.sum==y.sum&&x.chinese_and_math_sum!=y.chinese_and_math_sum)//如果总分相同,则比较语文和数学两科的总分,高者靠前;
{
return x.chinese_and_math_sum>y.chinese_and_math_sum;
}
if(x.sum==y.sum&&x.chinese_and_math_sum==y.chinese_and_math_sum&&x.chinese_and_math_max!=y.chinese_and_math_max)//如果仍相同,则比较语文和数学两科的最高分,高者靠前;
{
return x.chinese_and_math_max>y.chinese_and_math_max;
}
else
{
x.b=1;//如果仍相同,则二人并列。
y.b=1;//标记
}
}
bool cmp1(node x,node y)
{
return x.yuanlaidebianhao>y.yuanlaidebianhao;//排序函数2,用于将数组重新排成原来的样子,方便输出
}
signed main ()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].chinese>>a[i].math>>a[i].english;
a[i].sum=a[i].chinese+a[i].english+a[i].math;//同意赋值
a[i].chinese_and_math_sum=a[i].math+a[i].chinese;
a[i].yuanlaidebianhao=i;
a[i].chinese_and_math_max=max(a[i].chinese,a[i].math);
a[i].b=0;
}
sort(a+1,a+n+1,cmp);//第一次排序
int grade=1;//用于记录名次
int tot=0;//记录有几个人并列
bool flag=0;//一个旗帜
for(int i=1;i<=n;i++)
{
if(a[i].b==0)
{
a[i].xianzaidepaiming=grade;//如果当前没有并列,那么名次+1
grade++;
}
else
{
a[i].xianzaidepaiming=grade;//如果有并列,那么名次不变记录有几个人并列
tot++;
flag=1;//旗帜为1
}
if(flag==0)
{
grade+=tot;//名次需要加上中间并列的人数,如果没有并列tot为0名次不变,不影响程序
tot=0;
}
}
sort(a+1,a+n+1,cmp1);//第二次
for(int i=1;i<=n;i++)
{
cout<<a[i].xianzaidepaiming<<"\n";//输出
}
return 0;
}
//找不到问题,恳请大佬帮忙