3045. 学生信息处理

BillChen2000

这个题里有一群int装不下学号的学生

Minovak

醍醐灌顶

徐摆渡

不开ll见祖宗

lisha_cong

哇!

Li Dao

结构体排序

include

using namespace std;
struct Stu
{
long long SID;
string name;
int score[3];
};
int T;

vector V;

int cmp(const Stu& aa,const Stu& bb)
{
int x=aa.score[0]+aa.score[1]+aa.score[2];
int y=bb.score[0]+bb.score[1]+bb.score[2];
if(x!=y) return x>y;
else return aa.SID>n;

V.clear();
for(int i=1;i<=n;i++)
{
long long a;
int c,d,e;
string b;
cin>>a>>b>>c>>d>>e;
V.push_back((Stu){a,b,{c,d,e}});
}
sort(V.begin(),V.end(),cmp);
for(int i=0;i<V.size();i++)
printf(“%lld %s %d %d %d\n”,V[i].SID,V[i].name.c_str(),V[i].score[0],V[i].score[1],V[i].score[2]);
return;
}
int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

10175102262 LarsPendragon

哇!排序基础题6.8分!OJ总能带给我新惊喜。
懒得强转了,直接用字符串存+atof转浮点数算average。
附C代码:

#include <stdio.h>
#include <stdlib.h>
typedef struct {long long num; char name[10], score[3][5]; double ave;}Student;
int cmp(const void*a, const void*b)
{
    Student *p1=(Student*)a, *p2=(Student*)b;
    if(p1->ave==p2->ave) return p1->num>p2->num?1:-1;
    return p2->ave>p1->ave?1:-1;
}
int main()
{
    int T, I;
    scanf("%d",&T);
    for(I=0; I<T; I++)
    {
        int n, i;
        Student s[10];
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%lld %s %s %s %s",&s[i].num,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2]);
            s[i].ave=(atof(s[i].score[0])+atof(s[i].score[1])+atof(s[i].score[2]))/3;
        }
        qsort(s, n, sizeof(s[0]), cmp);
        printf("case #%d:\n",I);
        for(i=0; i<n; i++) printf("%lld %s %s %s %s\n",s[i].num,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2]);
    }
    return 0;
}
Deuchie

注意到平均数计算需要转浮点,因为一个学生不太可能得到爆 int 的分数,而且所有人都考三科,所以我们可以安全地假定:我们可以比较总和而不是平均数。
学号比较长,可以选择 string 或者 long long

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Student {
    string id;
    string name;
    unsigned score[3];
    unsigned sum;
};

void solve() {
    int n;
    cin >> n;
    vector<Student> students(n);
    for (auto &i : students) {
        cin >> i.id >> i.name >> i.score[0] >> i.score[1] >> i.score[2];
        i.sum = i.score[0] + i.score[1] + i.score[2];
    }

    // Here is a potential problem. The sum of the scores may overflow, but since
    // seldom is there any tests that rank so high a score, we may safely assume
    // that comparing sum is safe enough.
    sort(students.begin(), students.end(), [](Student const &a, Student const &b) {
        if (a.sum == b.sum)
            return a.id < b.id;
        return a.sum > b.sum;
    });

    // Newer C++ standards have enforced the string::data() method to present a
    // C-style string, we can safely use i.name.data() instead of i.name.c_str().
    // If you are using other languages, this assumption may not apply.
    for (auto const &i : students)
        printf("%s %s %u %u %u\n", i.id.data(), i.name.data(), i.score[0], i.score[1], i.score[2]);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        printf("case #%d:\n", i);
        solve();
    }
}
kingno

这题为啥这么多人没过,是因为我用python吗..

你当前正在回复 博客/题目
存在问题!