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;
}

提交结果:

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

相关推荐
LDR0065 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术5 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园5 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob5 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享5 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.5 天前
C语言--day30
c语言·开发语言
玖玥拾5 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
何以解忧,唯有..5 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽5 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下5 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php