首页 > 其他分享 >PAT Basic 1100. 校庆

PAT Basic 1100. 校庆

时间:2023-04-15 21:55:42浏览次数:34  
标签:PAT 校庆 int 校友 身份证号 1100 Basic const void

PAT Basic 1100. 校庆

1. 题目描述:

2019 年浙江大学将要庆祝成立 122 周年。为了准备校庆,校友会收集了所有校友的身份证号。现在需要请你编写程序,根据来参加校庆的所有人士的身份证号,统计来了多少校友。

2. 输入格式:

输入在第一行给出不超过 \(10^5\) 的正整数 N,随后 N 行,每行给出一位校友的身份证号(18 位由数字和大写字母X组成的字符串)。题目保证身份证号不重复。

随后给出前来参加校庆的所有人士的信息:首先是一个不超过 \(10^5\) 的正整数 M,随后 M 行,每行给出一位人士的身份证号。题目保证身份证号不重复。

3. 输出格式:

首先在第一行输出参加校庆的校友的人数。然后在第二行输出最年长的校友的身份证号 —— 注意身份证第 7-14 位给出的是 yyyymmdd 格式的生日。如果没有校友来,则在第二行输出最年长的来宾的身份证号。题目保证这样的校友或来宾必是唯一的。

4. 输入样例:

5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

5. 输出样例:

3
150702193604190912

6. 性能要求:

Code Size Limit
16 KB
Time Limit
800 ms
Memory Limit
64 MB

思路:

首先将校友和参加校庆的人的信息分别保存,然后遍历参加校庆的人判断是否为校友,将参加校庆的校友另外进行保存,这里相当于在校友信息中进行搜索,为了减少耗时,首先调用库函数qsort()将校友信息按身份证进行排序,然后调用库函数bsearch()进行二分搜索,最后根据是否有校友参加分情况对相关人员按照生日排序,输出最年长的身份证号。

这里需要特别注意的是排序函数的编写,涉及到二维数组的地址问题,容易出现Segmentation Fault。。。因为对二维数组名解引用得到的是一个一维数组对象,这一点目前搞的也不是很清楚。

My Code:

#include <stdio.h>
#include <stdlib.h> // qsort header, bsearch header, malloc header
#include <string.h> // strcmp header, strcpy header

#define MAX_NUM 100000

int sort_alu(const void *p1, const void *p2);
int sort_bsearch(const void *p1, const void *p2);
int sort_birth(const void *p1, const void *p2);

int main(void)
{
    char alumni[MAX_NUM][19] = {""};
    char guest[MAX_NUM][19] = {""};
    int alumniCount = 0;
    int guestCount = 0;
    char attendAlu[MAX_NUM][19] = {""};
    int aluAttendCount = 0;
    
    scanf("%d", &alumniCount);
    for(int i=0; i<alumniCount; ++i)
    {
        scanf("%s", alumni[i]);
        //printf("%s\n", alumni[i]);
    }
    
    scanf("%d", &guestCount);
    for(int i=0; i<guestCount; ++i)
    {
        scanf("%s", guest[i]);
        //printf("%s\n", guest[i]);
    }
    
    //char (*pAlu)[19] = (char (*)[19])malloc(sizeof(char [19]))
    char **pAlu = malloc(sizeof(char *) * alumniCount); // allocate heap memory 
    for(int i=0; i<alumniCount; ++i) // assign the pointer to alumni
    {
        pAlu[i] = alumni[i];
    }
    
    qsort(pAlu, alumniCount, sizeof(char *), sort_alu); // sort alumni by ID
    
//     for(int i=0; i<alumniCount; ++i) // output sort result
//     {
//         printf("%s\n", pAlu[i]);
//     }
    
    for(int i=0; i<guestCount; ++i)
    {
        //void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
        // bsearch have bug
        if(bsearch(guest[i], pAlu, alumniCount, sizeof(char *), sort_bsearch)) // this guest is alumni
        {
            strcpy(attendAlu[aluAttendCount++], guest[i]);
        }
    }
    
//     for(int i=0; i<aluAttendCount; ++i) // output attendAlu info
//     {
//         printf("%s\n", attendAlu[i]);
//     }
    
    printf("%d\n", aluAttendCount);
    if(aluAttendCount) // have alumni attend party
    {
        char **pRes = malloc(sizeof(char *) * aluAttendCount); // allocate heap memory 
        for(int i=0; i<aluAttendCount; ++i)
        {
            pRes[i] = attendAlu[i];
        }
        qsort(pRes, aluAttendCount, sizeof(char *), sort_birth); // sort by birth
        
        //qsort(attendAlu, aluAttendCount, sizeof(char *), sort_birth); // sort by birth
        printf("%s\n", pRes[0]);
//         for(int i=0; i<aluAttendCount; ++i) // output attendAlu info
//         {
//             printf("%s\n", attendAlu[i]);
//         }
        
        free(pRes);
    }
    else // doesn't have alumni attend party
    {
        char **pRes = malloc(sizeof(char *) * guestCount); // allocate heap memory
        for(int i=0; i<guestCount; ++i)
        {
            pRes[i] = guest[i];
        }
        qsort(pRes, guestCount, sizeof(char *), sort_birth); // sort by birth
        printf("%s\n", pRes[0]);
        
        free(pRes);
    }
    
    free(pAlu); // release heap memory
    return 0;
}

int sort_alu(const void *p1, const void *p2)
{
//     char (*pLeft)[19] = (char (*)[19])(*(char **)p1);
//     char (*pRight)[19] = (char (*)[19])(*(char **)p2);
    char (*pLeft)[19] = (*(char **)p1);
    char (*pRight)[19] = (*(char **)p2);
    
    return (strcmp(pLeft, pRight));
}

int sort_bsearch(const void *p1, const void *p2)
{
//     char (*pValue)[19] = (*(char **)p1); // this will cause segmentation fault
//     char (*pElem)[19] = (*(char **)p2);
    
    char *pValue = (char *)p1;
    char *pElem = *(char **)p2;
    
    return (strcmp(pValue, pElem));
}

int sort_birth(const void *p1, const void *p2)
{
    char *pLeft = *(char **)p1;
    char *pRight = *(char **)p2;
    
    return strcmp(pLeft+6, pRight+6);
}

标签:PAT,校庆,int,校友,身份证号,1100,Basic,const,void
From: https://www.cnblogs.com/tacticKing/p/17321997.html

相关文章

  • git 遇到的CApath: none问题解决
    在适应git时,遇到了如下问题。fatal:unabletoaccess'https://github.com/brunosimon/folio-2019.git/':errorsettingcertificateverifylocations: CAfile:D:/明月下/Git/mingw64/ssl/certs/ca-bundle.crtCApath:none第一反应是查找这个文件是什么,在不在。首先这......
  • PAT Basic 1099. 性感素数
    PATBasic1099.性感素数1.题目描述:“性感素数”是指形如\((p,p+6)\)这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自http://mathworld.wolfram.com/SexyPrimes.html)现给定一个整数,请你判断其是否为一个性感素数。2.输入格......
  • centos7 PATH 环境变量设置
    https://blog.csdn.net/qq_39715000/article/details/1250231901、系统环境变量系统环境变量对全部的用户生效,设置系统环境变量有三种方法。1)在/etc/profile文件中设置。用户登录时执行/etc/profile文件中设置系统的环境变量。但是,Linux不建议在/etc/profile文件中设置系统环......
  • os.path.dirname;os.path.abspath;os.walk方法详解
    os.path.dirname:os.path.dirname(path):用来获取文件的路径   os.path.dirname(__file__):用来获取当前py文件的上层目录例如:当前文件所处位置为:D:/AutoTestSys/script/AutoFunction/test1.pyprint(os.path.dirname(__file__))返回的结果为: D:/AutoTestSys/script/Aut......
  • PAT Basic 1097. 矩阵行平移
    PATBasic1097.矩阵行平移1.题目描述:给定一个 \(n×n\) 的整数矩阵。对任一给定的正整数 \(k<n\),我们将矩阵的奇数行的元素整体向右依次平移\(1、……、k、1、……、k、……\)个位置,平移空出的位置用整数 \(x\) 补。你需要计算出结果矩阵的每一列元素的和。2.输入格......
  • PAT Basic 1096. 大美数
    PATBasic1096.大美数1.题目描述:若正整数 \(N\) 可以整除它的4个不同正因数之和,则称这样的正整数为“大美数”。本题就要求你判断任一给定的正整数是否是“大美数”。2.输入格式:输入在第一行中给出正整数 \(K\)(\(≤10\)),随后一行给出 \(K\) 个待检测的、不超过 \(1......
  • 二进制patch工具xdelta的使用方法
     Xdelta是一个二进制的diff工具[同时又兼具了patch功能],diff和patch是Unix世界里很有用的一对工具:我们通常将它们结合起来实现生成补丁,应用补丁的目的。如果要处理的不是文本文件,是二进制文件,我们可以使用一个专门用来处理二进制文件的工具–xdelta。      Xdelta......
  • Getting Started / Basic Structures
    有序点云类似于图像,数据被分成行和列,其来源是立体相机或则TOF相机。其优点是最近邻操作的效率要高得多,从而加快了计算速度,降低了PCL中某些算法的成本。无序点云无序点云的width是点云的总数,height为1判断是无序点云还是有序点云的函数是isOrganized()......
  • TypeScript 报错:Type '({ filename: string; createTime: string; filePath: string;
    问题:因为TypeScript不支持直接给一个接口类型的变量赋一个未知的值。如consta:A={ name:'s'};你需要给这样的对象或数组值使用as指定一个类型。正确写法:consta:A={ name:'s'}asA;数组写法一样:consta:A[]=[ { name:'s' }]asA[];使用as将一......
  • UVA 12295 Optimal Symmetric Paths 最短路求方案数
    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23587题意:给一个n*n的矩阵,每个方格中有一个数字,从左上角走到右下角,且路径必须关于副对角线对称,求使路线上数字和最小的方案数思路:既然要关于副对角线对称,那么可以把关于副对角线对称的方格的值加到一起去,这样就......