• C/C++数据结构——QQ帐户的申请与登陆(散列表)


    活动地址:CSDN21天学习挑战赛
    学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。

    题目描述

    实现QQ新帐户申请和老帐户登陆的简化版功能。最大挑战是:据说现在的QQ号码已经有10位数了。

    输入格式

    输入首先给出一个正整数N(≤105 ),随后给出N行指令。每行指令的格式为:“命令符(空格)QQ号码(空格)密码”。其中命令符为“N”(代表New)时表示要新申请一个QQ号,后面是新帐户的号码和密码;命令符为“L”(代表Login)时表示是老帐户登陆,后面是登陆信息。QQ号码为一个不超过10位、但大于1000(据说QQ老总的号码是1001)的整数。密码为不小于6位、不超过16位、且不包含空格的字符串。

    输出格式

    针对每条指令,给出相应的信息:
    1)若新申请帐户成功,则输出“New: OK”;
    2)若新申请的号码已经存在,则输出“ERROR: Exist”;
    3)若老帐户登陆成功,则输出“Login: OK”;
    4)若老帐户QQ号码不存在,则输出“ERROR: Not Exist”;
    5)若老帐户密码错误,则输出“ERROR: Wrong PW”。

    输入样例

    5
    L 1234567890 myQQ@qq.com
    N 1234567890 myQQ@qq.com
    N 1234567890 myQQ@qq.com
    L 1234567890 myQQ@qq
    L 1234567890 myQQ@qq.com

    输出样例

    ERROR: Not Exist
    New: OK
    ERROR: Exist
    ERROR: Wrong PW
    Login: OK

    说明

    代码长度限制 16 KB 时间限制 1200 ms 内存限制 64 MB
    来源PTA:https://pintia.cn/problem-sets/15/problems/723

    解题代码

    #include
    #include
    #define hashKey 100000
    
    typedef struct node{
        long long account;
        char pwd[20];
        struct node *next;
    }hashNode;
    typedef struct{
        hashNode *root;
    }hashTable;
    hashTable table[hashKey];
    void init()
    {
        for(int i=0;i<hashKey;i++){
            table[i].root=(hashNode*)malloc(sizeof(hashNode));
            table[i].root->next=NULL;
        }
    }
    int Hash(long long a)
    {
        return a%hashKey;
    }
    void Login(long long a,char *pwd)
    {
        int key = Hash(a);
        if(table[key].root->next==NULL){
            printf("ERROR: Not Exist\n");
            return;
        }else{
            hashNode *p=table[key].root->next;
            while(p!=NULL){
                if(p->account==a){
                    if(strcmp(p->pwd,pwd)==0){
                        printf("Login: OK\n");
                    }else{
                        printf("ERROR: Wrong PW\n");
                    }
                    return;
                }
                p=p->next;
            }
            printf("ERROR: Not Exist\n");
        }
    }
    void New(long long a,char *pwd)
    {
        int key = Hash(a);
        hashNode *p=table[key].root->next;
        hashNode *q=table[key].root;
        while(p!=NULL)
        {
            if(p->account==a){
                printf("ERROR: Exist\n");
                return;
            }
            p=p->next;
            q=q->next;
        }
        hashNode *t = (hashNode*)malloc(sizeof(hashNode));
        t->account=a;
        strcpy(t->pwd,pwd);
        q->next=t;
        t->next=NULL;
        printf("New: OK\n");
        
    }
    int main()
    {
        int n;
        long long a;
        char pwd[20],c;
        while(scanf("%d",&n)!=EOF){
            getchar();
            init();
            for(int i=0;i<n;i++){
                scanf("%c %lld %s",&c,&a,pwd);
                getchar();
                if(c=='L'){
                    Login(a,pwd);
                }else{
                    New(a,pwd);
                }
            }
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    解题思路

    就是普通的散列链表,注意一下输出格式和c语言字符输入后需要吸收回车,其他没了,没有用到探测,直接放到节点链表了,然后我数据开的比较小的时候会超时,所以就开大了直接通过了,牺牲内存的做法属于是。用map同样可以做,解题思路多样。

  • 相关阅读:
    从权限系统的菜单管理看算法和数据结构
    数据结构与算法:括号生成
    spring boot集成flyway快速入门demo
    虽然webpack4以后。webpack 可以不用再引入一个配置文件来打包项目,但还是梳理常用配置信息
    cJson 学习笔记
    一文详解 jitpack 多渠道maven库发布
    高等数学(第七版)同济大学 习题4-4(后14题) 个人解答
    2311rust,到74版本更新
    青少年python系列 46.文件操作2
    微信小程序报request:fail url not in domain list的解决方法
  • 原文地址:https://blog.csdn.net/weixin_44546342/article/details/126361418