PAT-AL 1039. Course List for Student

本文介绍了一种基于C语言的课程选修查询系统的设计与实现。系统通过定义课程和学生结构体,采用排序和二分查找算法提高查询效率。文章详细展示了如何使用结构体、数组和标准库函数实现高效的数据管理和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、知识点:排序
2、思路:定义course结构、student结构:
struct course{
int ind, stu_num; //课程编号,选课学生数
char name_arr[MAXNI][STRLEN]; //学生姓名
};

struct student{
int order; //查询顺序
char name[STRLEN]; //姓名
vector course_ind; //课程编号
};
读入课程数组cour,然后按ind排序;读入查询学生数组stu_arr,按姓名排序。然后遍历课程,对于每门课的每个选课学生,按照二分查找查询学生表stu_arr中的姓名,如果找到,就将对应的课程编号加入该学生。最后将查询学生表stu_arr做数据离散化,映射到disc数组,遍历disc数组打印输出答案。

注意:
①尽量用C的内容而不是C++(代码换成C++版超时,而C版顺利AC,~~);
②学生姓名排好序按照二分查找提升效率,否则会超时;
③student结构中用了vector,如果用malloc分配内存会出错,直接定义stu_arr数组或者用new分配内存则没事。

/*用途:
**说明:
**算法:
*/

//#define LOCAL
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 40000+10
#define MAXK 2500+10
#define MAXNI 200+10
#define STRLEN 10
struct student{
    int order;
    char name[STRLEN];
    vector<int> course_ind;
};

struct course{
    int ind, stu_num;
    char name_arr[MAXNI][STRLEN];
};
int N, K;
course *cour;
//student stu_arr[MAXN];
student *stu_arr;
int *disc;    //stu_arr的离散化数组

void init_course();
int cmp_course_ind(const void* a, const void* b);
void init_stu();
int cmp_order(const void* a, const void* b);
int find_name(char* name);    //二分查找姓名
void print_course_ind(int ind);
int main()
{
#ifdef LOCAL
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif

    scanf("%d%d", &N, &K);
    cour = (course*)malloc(sizeof(course)*K);
    init_course();
    for(int i=0; i<K; i++){
        int ind, stu_num;
        char name[STRLEN];
        scanf("%d%d", &ind, &stu_num);
        for(int j=0; j<stu_num; j++){
            scanf("%s", name);
            cour[i].ind = ind;
            strcpy(cour[i].name_arr[cour[i].stu_num], name);
            cour[i].stu_num++;
        }
    }

    qsort(cour, K, sizeof(course), cmp_course_ind); 
//  stu_arr = (student*)malloc(sizeof(student)*N);  //出错 
    stu_arr = new student[N];
    for(int i=0; i<N; i++){
        scanf("%s", stu_arr[i].name);
        stu_arr[i].order = i;
    }

    qsort(stu_arr, N, sizeof(student), cmp_order);

    int index;
    char name[STRLEN];
    for(int i=0; i<K; i++){
        for(int j=0; j<cour[i].stu_num; j++){
            index = find_name(cour[i].name_arr[j]);
            if(index != -1)
                stu_arr[index].course_ind.push_back(cour[i].ind);
        }
    }

    disc = new int[N];
    for(int i=0; i<N; i++)
        disc[stu_arr[i].order] = i;

    for(int i=0; i<N; i++){
        int ind = disc[i];
        printf("%s %d", stu_arr[ind].name, stu_arr[ind].course_ind.size());
        print_course_ind(ind);
    }

    return 0;
}

void init_course()
{
    for(int i=0; i<K; i++)
        cour[i].ind = cour[i].stu_num = 0;
}

void print_course_ind(int ind)
{
    int len = stu_arr[ind].course_ind.size();
    for(int i=0; i<len; i++)
        printf(" %d", stu_arr[ind].course_ind[i]);
    printf("\n");
}

inline int cmp_course_ind(const void* a, const void* b)
{
    return (*(course*)a).ind - (*(course*)b).ind;
}

inline int cmp_order(const void* a, const void* b)
{
    return strcmp((*(student*)a).name, (*(student*)b).name) < 0 ? -1 : 1;   //注意这里的比较字符数组的方法
}

inline int find_name(char* name)
{
    int start, med, end;
    start = 0;
    end = N-1;
    med = (start + end) / 2;
    while(start <= end){
        if(strcmp(name, stu_arr[med].name) > 0)
            start = med+1;
        else if(!strcmp(name, stu_arr[med].name))
            return med;
        else
            end = med-1;

        med = (start + end) / 2;
    }
    return -1;
}
<think>嗯,我现在遇到了Git推送时出现403权限被拒绝的问题,得想办法解决。首先,我记得错误信息大概是说没有权限,可能是公钥的问题或者认证失败。根据用户提供的引用,有几个可能的原因和解决办法。 先看引用[1]和[2],里面提到的是Permission denied (publickey),这通常和SSH密钥有关。用户可能没有正确配置SSH密钥,或者密钥没有被添加到GitHub账户中。解决方法是检查SSH密钥是否存在,生成新的密钥对,然后添加到GitHub的SSH设置里。另外,引用[4]提到要使用默认的id_rsa文件名,可能用户自己修改了密钥文件名导致Git找不到正确的密钥。 然后是引用[3],用户遇到了403错误,使用的是HTTPS协议,这时候可能认证信息有误,或者远程仓库的URL不正确。可能需要检查远程仓库的URL是否属于自己,或者切换成SSH协议来避免认证问题。另外,缓存的身份凭证可能过期或错误,需要清除或者更新。 现在用户的问题可能有两种情况:使用SSH时的公钥问题,或者使用HTTPS时的认证问题。需要分别排查。比如,先检查使用的协议是SSH还是HTTPS,可以通过git remote -v查看远程仓库的URL。如果是HTTPS的URL,可能会因为账号密码错误或者双因素认证的问题导致403。而如果是SSH的URL,则可能是公钥未正确配置。 如果是SSH的问题,步骤应该是生成SSH密钥,添加到ssh-agent,再添加到GitHub账户。如果密钥已经存在,可能需要启动ssh-agent,或者检查权限是否正确,比如密钥文件的权限是否过于开放(应该设置为600)。 如果是HTTPS的问题,可能需要更新凭据,或者检查远程仓库的URL是否正确,是否有权限访问。例如,如果克隆的是别人的仓库,推送时就会没有权限,需要确认仓库归属。另外,使用个人访问令牌(PAT)替代密码也是一个解决办法,因为GitHub已经不再支持密码认证。 还有可能的情况是,用户同时有多个Git账户,导致密钥冲突,这时候需要配置SSH的config文件来指定不同的密钥。 综上,可能的解决步骤包括:确认协议类型,检查密钥配置,更新远程URL为SSH,清除或更新凭据,使用访问令牌,检查仓库权限等。需要一步步排查,根据错误信息的具体情况选择对应的解决方法。</think>### Git推送出现403权限被拒绝的解决方案 #### **1. 检查远程仓库URL类型(SSH/HTTPS)** 首先确认使用的协议类型: ```bash git remote -v ``` - 若URL以`https://`开头,可能出现认证问题;若以`git@github.com:`开头,则属于SSH协议。 - **HTTPS协议问题**:403错误通常与账号权限或缓存凭据有关。 - **SSH协议问题**:需检查SSH密钥配置。 --- #### **2. 针对HTTPS协议的解决方法** ##### (1) 清除缓存凭据 Git会缓存旧密码或令牌,导致认证失败: ```bash git credential-osxkeychain erase host=github.com protocol=https ``` 或手动删除钥匙串中的GitHub条目(Mac)[^3]。 ##### (2) 使用个人访问令牌(PAT)替代密码 GitHub已禁用密码推送,需生成PAT: 1. 进入GitHub **Settings → Developer settings → Personal access tokens**。 2. 生成新令牌(勾选`repo`权限)。 3. 推送时用令牌代替密码输入。 ##### (3) 检查仓库归属权限 若URL指向他人仓库(如`sweetbao/Machine-Reading...`),需修改为你的仓库地址: ```bash git remote set-url origin https://github.com/你的用户名/仓库名.git ``` --- #### **3. 针对SSH协议的解决方法** ##### (1) 生成并添加SSH密钥 ```bash # 生成新密钥(默认路径和文件名) ssh-keygen -t ed25519 -C "your_email@example.com" # 启动ssh-agent并添加密钥 eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ``` 将公钥(`~/.ssh/id_ed25519.pub`)粘贴到GitHub **Settings → SSH and GPG keys**[^1][^4]。 ##### (2) 检查SSH连接 ```bash ssh -T git@github.com ``` 若显示`You've successfully authenticated`,则配置成功。 --- #### **4. 其他常见问题** - **密钥权限问题**:确保私钥文件权限为`600`: ```bash chmod 600 ~/.ssh/id_rsa ``` - **多账户冲突**:通过`~/.ssh/config`指定不同仓库的密钥: ``` Host github.com-user1 HostName github.com User git IdentityFile ~/.ssh/user1_key Host github.com-user2 HostName github.com User git IdentityFile ~/.ssh/user2_key ``` 修改仓库URL为对应Host名(如`git@github.com-user1:user1/repo.git`)。 --- #### **总结步骤** 1. 确认URL类型并切换协议(推荐SSH)。 2. 检查密钥配置或生成新密钥。 3. 清除HTTPS缓存凭据或使用PAT。 4. 验证仓库归属权和访问权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值