PTA题解 --- 静静的推荐(C语言)

今天是PTA题库解法讲解的第七天,今天我们要讲解静静的推荐,题目如下:

解题思路:

这个问题的核心在于如何在满足给定条件的情况下,最大化推荐学生的数量。首先,我们需要过滤出所有天梯赛成绩不低于175分的学生。然后,我们要按天梯赛成绩排序,如果天梯赛成绩相同,再根据PAT成绩排序。在推荐学生时,我们需要按批次进行,确保每一批的成绩严格递增,同时如果同一天梯赛成绩的学生PAT成绩达到了企业的面试分数线,也可以被接受。

代码实现:

复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int ladderScore;
    int patScore;
} Student;

int compareStudents(const void *a, const void *b) {
    Student *studentA = (Student *)a;
    Student *studentB = (Student *)b;
    if (studentA->ladderScore == studentB->ladderScore) {
        return studentB->patScore - studentA->patScore; // Descending PAT scores
    }
    return studentA->ladderScore - studentB->ladderScore; // Ascending Ladder scores
}

int main() {
    int N, K, S;
    scanf("%d %d %d", &N, &K, &S);
    Student students[N];
    int validStudents = 0;

    for(int i = 0; i < N; i++) {
        scanf("%d %d", &students[i].ladderScore, &students[i].patScore);
        if (students[i].ladderScore >= 175) {
            validStudents++;
        } else {
            students[i].ladderScore = -1; // Mark invalid students
        }
    }

    // Sort students based on Ladder and PAT scores
    qsort(students, N, sizeof(Student), compareStudents);

    // Logic to count recommended students goes here.
    // This is a simplified placeholder for the complex logic required.
    // You'll need to implement the detailed selection criteria as described.

    printf("%d\n", validStudents); // Placeholder for actual count of recommended students
    return 0;
}

提交结果:

本题部分没有通过,小伙伴们可以在评论区讨论,来个最优解哦~

相关推荐
不吃土豆的马铃薯4 分钟前
高性能服务器程序框架详解(包括Reactor,有限状态机等)
linux·服务器·开发语言·网络·c++
bucenggaibian5 分钟前
搭建CMD编译C语言环境
linux·c语言·windows
Shadow(⊙o⊙)6 分钟前
库的制作与原理1.0,库打包,协作,目标文件.o、ELF格式。
linux·运维·服务器·开发语言
wyc是xxs6 分钟前
用纯 Node.js 写了一个 JS 解释器 — kernel-js-lite
开发语言·javascript·npm·node.js
hai3152475437 分钟前
AI工业化编程的黎明:由逻辑压缩到知识融合的范式跃迁
开发语言·人工智能·线性代数·机器学习·数学建模·概率论
Cloud_Shy61814 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第一章 Item 7 - 9)
开发语言·数据库·python
এ慕ོ冬℘゜15 分钟前
从零封装企业级通用确认弹窗组件|高复用、低耦合、适配全场景
开发语言·前端·javascript
郝学胜-神的一滴15 分钟前
Qt 高级开发 020:水平布局手写代码实战
开发语言·c++·qt·系统架构·软件构建·用户界面
Mortalbreeze23 分钟前
C++11 ---- 右值引用、值类型
开发语言·c++
東隅已逝,桑榆非晚24 分钟前
新手入门指南:认识 C 语言文件操作(下)
c语言·笔记