1080. Graduate Admission (30)【排序】——PAT (Advanced Level) Practise

本文介绍了一个用于自动处理研究生招生流程的程序设计。该程序能够根据考生的考试成绩和面试成绩进行排名,并根据考生的志愿顺序及学校的招生名额来确定录取结果。

题目信息

1080. Graduate Admission (30)

时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.

Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE + GI) / 2. The admission rules are:

The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one’s turn to be admitted; and if the quota of one’s most preferred shcool is not exceeded, then one will be admitted to this school, or one’s other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.
Input Specification:

Each input file contains one test case. Each case starts with a line containing three positive integers: N (<=40,000), the total number of applicants; M (<=100), the total number of graduate schools; and K (<=5), the number of choices an applicant may have.

In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.

Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant’s GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M-1, and the applicants are numbered from 0 to N-1.

Output Specification:

For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants’ numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

Sample Input:
11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4
Sample Output:
0 10
3
5 6 7
2 8

1 4

解题思路

仔细分析多种排序即可

AC代码

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int school[105], pree[105], prei[105];
vector<int> res[105];
struct People{
    int ge, gi, gfinal, s[5], id, rank;
    bool operator<(const People& b) const{
        if (gfinal == b.gfinal){
            return ge > b.ge;
        }
        return gfinal > b.gfinal;
    }
    bool operator==(const People& b) const{
        return gfinal == b.gfinal && ge == b.ge;
    }
}peo[40005];
int main()
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; ++i){
        scanf("%d", school + i);
    }
    for (int i = 0; i < n; ++i){
        scanf("%d%d", &peo[i].ge, &peo[i].gi);
        for (int j = 0; j < k; ++j){
            scanf("%d", &peo[i].s[j]);
        }
        peo[i].id = i;
        peo[i].gfinal = peo[i].ge + peo[i].gi;
    }
    sort(peo, peo + n);
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < k; ++j){
            int sid = peo[i].s[j];
            if (school[sid] > 0 || (pree[sid] == peo[i].ge && prei[sid] == peo[i].gi)){
                --school[sid];
                res[sid].push_back(peo[i].id);
                pree[sid] = peo[i].ge;
                prei[sid] = peo[i].gi;
                break;
            }
        }
    }
    for (int i = 0; i < m; ++i){
        if (res[i].size() > 0){
            sort(res[i].begin(), res[i].end());
            printf("%d", res[i][0]);
            for (int j = 1; j < res[i].size(); ++j){
                printf(" %d", res[i][j]);
            }
        }
        printf("\n");
    }
    return 0;
}
<think>我们遇到了文件路径错误:'No such file or directory: 'Admission_Predict.csv''。这通常意味着Python在当前工作目录下找不到指定的文件。让我们逐步解决这个问题。 ### 解决步骤: 1. **确认文件存在**: - 首先,确保你已经下载了`Admission_Predict.csv`文件,并且知道它存储在你计算机上的位置。 2. **检查当前工作目录**: - Python在运行时有一个当前工作目录(Current Working Directory, CWD),默认情况下,它会在该目录中查找文件。你可以通过以下代码检查当前工作目录: ```python import os print(os.getcwd()) ``` 3. **将文件放在正确的位置**: - 最简单的方法是将CSV文件放在当前工作目录下。 - 或者,你可以使用绝对路径来指定文件的位置,例如: ```python data = pd.read_csv('C:/Users/YourName/Documents/Admission_Predict.csv') ``` - 注意:在Windows系统中,路径分隔符可以使用`/`或`\\`(双反斜杠)以避免转义问题。 4. **更改当前工作目录**: - 如果你知道文件所在的目录,可以更改当前工作目录: ```python os.chdir('path_to_directory') # 例如:os.chdir('C:/Users/YourName/Documents') ``` 5. **使用相对路径**: - 如果CSV文件位于当前工作目录的子文件夹中,比如`data`文件夹,则路径应为: ```python data = pd.read_csv('data/Admission_Predict.csv') ``` ### 示例代码修正: 假设你的CSV文件位于与Python脚本相同的目录,那么直接使用文件名即可。但如果不确定,建议使用绝对路径。 ```python import pandas as pd import os # 方法1:使用绝对路径(替换为你的实际路径) # file_path = 'C:/Users/YourUsername/Documents/Admission_Predict.csv' # 方法2:将文件放在当前工作目录,然后直接读取 file_path = 'Admission_Predict.csv' # 检查文件是否存在 if os.path.exists(file_path): data = pd.read_csv(file_path) print("文件读取成功!") else: print(f"错误:文件 '{file_path}' 不存在。请检查路径。") # 打印当前工作目录以便调试 print(f"当前工作目录:{os.getcwd()}") ``` ### 注意事项: - 在Windows系统中,路径可以使用正斜杠`/`或双反斜杠`\\`(因为反斜杠在Python字符串中是转义字符)。 - 在Mac/Linux系统中,路径使用正斜杠`/`。 ### 调试提示: 如果上述方法仍无法解决,请尝试: - 检查文件名拼写(包括大小写,特别是在Linux系统中)。 - 检查文件扩展名(有时可能是`.txt`或`.csv`隐藏了,或者实际为`.xlsx`格式)。 ### 相关问题: §§相关问题§§ 1. 如何在Python中获取当前工作目录? 2. 如何修改Python的当前工作目录? 3. 在Python中如何处理文件路径的跨平台兼容性? 4. 如何检查一个文件或目录是否存在? 5. 使用Pandas读取CSV文件时还有哪些常见错误及解决办法? </think>### Python报错 `No such file or directory: 'Admission_Predict.csv'` 的解决办法 这个错误表明Python无法找到你指定的CSV文件。以下是逐步解决方案: --- #### 1. **确认文件存在性** ```python import os # 检查文件是否存在 if not os.path.exists('Admission_Predict.csv'): print("错误: 文件不存在!请检查文件路径") else: print("文件存在,准备读取") ``` --- #### 2. **检查当前工作目录** ```python import os # 打印当前工作目录 print(f"当前工作目录: {os.getcwd()}") # 列出目录内容 print("目录内容:", os.listdir()) ``` - ✅ 如果文件不在输出列表中,说明文件不在当前目录 - ✅ 需将文件移动到打印的目录路径中,或使用绝对路径 --- #### 3. **使用绝对路径替代(推荐)** ```python import pandas as pd # Windows示例 data = pd.read_csv(r'C:\Users\YourName\Documents\Admission_Predict.csv') # Mac/Linux示例 data = pd.read_csv('/Users/YourName/Documents/Admission_Predict.csv') ``` > 路径前的`r`表示原始字符串(Windows需要)[^1] --- #### 4. **路径拼接技巧** ```python import os import pandas as pd # 动态构建路径 base_dir = r'C:\ProjectFolder\data' # 你的实际目录 file_path = os.path.join(base_dir, 'Admission_Predict.csv') data = pd.read_csv(file_path) ``` > 使用`os.path.join()`保证跨平台兼容性[^2] --- #### 5. **验证文件权限** ```python import os # 检查文件权限 file_path = 'Admission_Predict.csv' if os.path.exists(file_path): print("可读权限:", os.access(file_path, os.R_OK)) print("可写权限:", os.access(file_path, os.W_OK)) ``` - 🔒 如果返回`False`,需修改文件权限 - 🖥️ Windows:右键文件 > 属性 > 安全 - 🐧 Linux/Mac:`chmod +r Admission_Predict.csv` --- ### 常见错误原因总结 | 原因 | 解决方法 | |------|----------| | 文件不在当前目录 | 移动文件或使用绝对路径 | | 文件名拼写错误 | 检查大小写和扩展名(.csv) | | 路径分隔符错误 | Windows用`\`或`/`,Mac/Linux用`/` | | 权限不足 | 修改文件权限 | | IDE工作目录设置 | 在IDE中设置正确工作目录 | --- ### 预防性编程技巧 ```python import os import pandas as pd def safe_read_csv(file_name): """安全读取CSV文件""" if not os.path.exists(file_name): raise FileNotFoundError(f"❌ 文件不存在: {file_name}\n当前目录: {os.getcwd()}") if not file_name.endswith('.csv'): print(f"⚠️ 警告: 文件扩展名非.csv") return pd.read_csv(file_name) # 使用示例 try: data = safe_read_csv('Admission_Predict.csv') except FileNotFoundError as e: print(e) ``` > 此函数提供详细的错误诊断信息[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值