题目
分析
sort实现排序与排名即可。(具体内容后续补充,最近太忙了QAQ)
这里注意scanf_s在输入字符串时,要在变量名后加上缓冲区大小,比如scanf_s("%s %d", stu[i][p].stunum, 14, &stu[i][p].score);
,这样是正确的;
而scanf_s("%s %d", stu[i][p].stunum, &stu[i][p].score);
会出错。这是因为scanf_s更严格。
代码
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct node {
char stunum[30];
int locanum;
int score;
int localrank;
int finalrank;
}snode;
node stu[110][310];
node all[40000];
int eachsum[110];//记录每个考场的学生人数
bool cmp0(node a, node b)
{
if (a.score != b.score)
return a.score > b.score;
else return strcmp(a.stunum ,b.stunum)<0;
}
int main()
{
int lsum;//总共的考场数量
scanf_s("%d", &lsum);
int m = lsum;
for (int i = 1;i <= m;i++)//每个考场遍历
{
scanf_s("%d", &eachsum[i]);
int p = 0;
int temp = eachsum[i];
while (temp--)
{
scanf_s("%s %d", stu[i][p].stunum, 14, &stu[i][p].score);
stu[i][p].locanum = i;
p++;
}
}
for (int i = 1;i <= m;i++)
{
sort(stu[i], stu[i] + eachsum[i], cmp0);
}
for (int i = 1;i <= m;i++)//考场排名
{
stu[i][0].localrank = 1;
for (int p = 1;p < eachsum[i];p++)
{
if (stu[i][p].score == stu[i][p - 1].score) stu[i][p].localrank = stu[i][p - 1].localrank;
else stu[i][p].localrank = p + 1;
}
}
int h = 0;
for (int i = 1;i <= m;i++)
{
for (int p = 0;p < eachsum[i];p++)
{
all[h] = stu[i][p];
h++;
}
}
sort(all, all + h, cmp0);//总排名
all[0].finalrank = 1;
for (int i = 1;i < h;i++)
{
if (all[i].score == all[i-1].score) all[i].finalrank = all[i-1].finalrank;
else all[i].finalrank = i + 1;
}
printf("%d\n", h);
for (int i = 0;i < h;i++)
{
printf("%s %d %d %d\n", all[i].stunum, all[i].finalrank, all[i].locanum, all[i].localrank);
}
}