2849. 成绩排序

spongebob

Python3 多条件排序

n = int(input())

grades = []
for _ in range(n):
    s_id, s_grade = input().split()
    s_grade = int(s_grade)
    if s_grade>=60:
        grades.append([s_id,s_grade])

grades = sorted(grades, key=lambda x: (x[1], -int(x[0])), reverse=True)

for i in range(len(grades)):
    print(f"{grades[i][0]} {grades[i][1]}")
╮ 潜心 ╰

第一次写优先队列……
#include
using namespace std;

struct student {string num; int score;};
struct cmp {
    bool operator() (const student &a, const student &b) {
        if (a.score != b.score) return a.score < b.score;
        else return a.num > b.num;
    }
};
int main() {
    //freopen("testin.txt", "r", stdin);
    //freopen("testout.txt", "w", stdout);
    int n;
    student tmp;
    priority_queue <student, vector<student>, cmp> p;
    cin >> n;
    for (int i=0; i<n; ++i) {
        cin >> tmp.num >> tmp.score;
        p.push(tmp);
    }
    while (!p.empty() && p.top().score >= 60) {
        cout << p.top().num << ' ' << p.top().score << endl;
        p.pop();
    }
    return 0;
}

最后摆上C的

include

#include <string.h>
#include <stdlib.h>
typedef struct {char num[12]; int score;} stu;
int cmp (const void *a, const void *b) {
    stu *x = (stu *)a;
    stu *y = (stu *)b;
    if (x->score != y->score) return y->score - x->score;
    else return strcmp(x->num, y->num);
}
int main() {
    //freopen("testin.txt", "r", stdin);
    //freopen("testout.txt", "w", stdout);
    int n, i;
    stu a[101];
    scanf("%d", &n);
    for (i=0; i<n; ++i)
        scanf("%s%d", a[i].num, &a[i].score);
    qsort(a, n, sizeof(a[0]), cmp);
    for (i=0; i<n; ++i) {
        if (a[i].score < 60) break;
        printf("%s %d\n", a[i].num, a[i].score);
    }
    return 0;
}
妈耶

输入a[i].num的时候,前面有没有&都能ac是为什么?

10185101178

include

include

include

typedef struct
{
char num[13];
int score;
}
stu;

int cmp (const void a, const void b) {
stu x = (stu )a;
stu y = (stu )b;
if(x->score == y->score)
{
return strcmp(x->num , y->num);
}
else return y->score - x->score;
}
int main()
{
int n,i;
stu a[101];
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%s %d”,a[i].num,&a[i].score);
}

qsort(a,n,sizeof(a[0]),cmp);


for(i=0;i<n;i++)
{
    if(a[i].score<60) continue;
    printf("%s %d\n",a[i].num,a[i].score);

}
return 0;

}

YZAZJL

include

include

using namespace std;

struct stu{
long long num;
int score;
};

bool cmp(stu a, stu b){
if(a.score == b.score){
return a.num < b.num; // 非降序
}
return a.score > b.score;// 非升序
}

int main()
{
int n;
cin >> n;
int i;
stu a[n];
for(i = 0; i < n; i++){
cin >> a[i].num >> a[i].score;
}
sort(a, a + n, cmp);
for(i = 0; i < n; i++){
if(a[i].score >= 60){
cout << a[i].num << ” ” << a[i].score << endl;
}
}
return 0;
}

YZAZJL

冒泡排序

include

include

include

using namespace std;

int main()
{
int n;
cin >> n;
int i;
long long a[n][2];
for(i = 0; i < n; i++){
long long num, grade;
cin >> num >> grade;
a[i][0] = num;
a[i][1] = grade;
}
int j;
int flag = 0;
for(i = 0; i < n; i++){
flag = 0;
for(j = n - 1; j > i; j–){
if(a[j][1] > a[j-1][1]){
long long tmp_n, tmp_g;
tmp_n = a[j][0];
tmp_g = a[j][1];
a[j][0] = a[j-1][0];
a[j][1] = a[j-1][1];
a[j-1][0] = tmp_n;
a[j-1][1] = tmp_g;
}
if(a[j][1] == a[j-1][1]){
if(a[j][0] < a[j-1][0]){
long long tmp_n, tmp_g;
tmp_n = a[j][0];
tmp_g = a[j][1];
a[j][0] = a[j-1][0];
a[j][1] = a[j-1][1];
a[j-1][0] = tmp_n;
a[j-1][1] = tmp_g;
}
}
}
}
for(i = 0; i < n; i++){
if(a[i][1] >= 60){
cout << a[i][0] << ” ” << a[i][1] << endl;
}
}
return 0;
}

Li Dao

强烈安利C++的sort
即使只会c,也可以用sort

第一种:重载小于号的写法

include

using namespace std;
int n;
struct Stu
{
string SID;
int score;
bool operator<(const Stu& bb) const
{
if(score!=bb.score) return score>bb.score;
else return SID<bb.SID;
}
};
vector V;

int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
string x;int y;
cin>>x>>y;
if(y>=60) V.push_back((Stu){x,y});
}
sort(V.begin(),V.end());
for(int i=0;i<V.size();i++)
cout<<V[i].SID<<’ ‘<<V[i].score<<endl;
return 0;
}

第二种:写cmp函数

include

using namespace std;
int n;
struct Stu
{
string SID;
int score;
};
vector V;
int cmp(const Stu& aa,const Stu& bb)
{
if(aa.score!=bb.score) return aa.score>bb.score;
else return aa.SID>n;
for(int i=1;i<=n;i++)
{
string x;int y;
cin>>x>>y;
if(y>=60) V.push_back((Stu){x,y});
}
sort(V.begin(),V.end(),cmp);
for(int i=0;i<V.size();i++)
cout<<V[i].SID<<’ ‘<<V[i].score<<endl;
return 0;
}

当然这里还用到了C++中的string,会好理解很多

召唤师天下第一

题目中说了是11位数,所以我的long long,但是这样只能过9个用例,改成14位的字符串能过10个.这是为什么呢

召唤师天下第一

0打头啊真的蠢。证据确凿,百口莫辩。

召唤师天下第一

我死了,连续四道题差一个样例,这种情况真的是烦。

╮ 潜心 ╰

顺便吐槽一下:垃圾界面 代码放上来没缩进还看不到头文件:)

Fifnmar

大佬,EOJ 是 Markdown 格式的

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