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

提交结果:

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

相关推荐
祈安_3 天前
C语言内存函数
c语言·后端
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054965 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy87874755 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月5 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237175 天前
C语言-数组练习进阶
c语言·开发语言·算法