• 5-15 QQ帐户的申请与登陆 (25分) HASH


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

    输入格式:

    输入首先给出一个正整数NN(le 10^5105​​),随后给出NN行指令。每行指令的格式为:“命令符(空格)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
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    #define MAXN 17
    typedef long long LL;
    
    typedef struct node
    {
        LL id;
        char password[MAXN];
        struct node* next;
    }*List;
    
    typedef struct tb
    {
        LL size;
        List *list;
    }*Hashlist;
    int NextPrim(int x)
    {
        int j;
        for(int t=x;;t++)
        {
            for(j=2;j*j<=t;j++)
                if(t%j==0)
                    break;
            if(j*j>t)
                return t;
        }
    }
    int Hash(LL id,LL size)
    {
        //cout<<id%size<<endl;
        return id%size;
    }
    Hashlist Init(LL size)
    {
        Hashlist H = (Hashlist)malloc(sizeof(tb));
        H->size = size;
        H->list = (List*)malloc(sizeof(List)*H->size);
        for(int i=0;i<H->size;i++)
        {
            H->list[i] = (List)malloc(sizeof(node));
            H->list[i]->next = NULL;
        }
        return H;
    }
    List Find(LL id,char pw[],Hashlist H)
    {
        List t = H->list[Hash(id,H->size)];
        List p=t->next;
        while(p!=NULL && p->id!=id)
            p = p->next;
        return p;
    }
    void Insert(LL id,char pw[],Hashlist H)
    {
        List f = Find(id,pw,H);
        if(f==NULL)
        {
            List t = H->list[Hash(id,H->size)];
            List tmp = (List)malloc(sizeof(node));
            tmp->id = id;
            strcpy(tmp->password,pw);
            tmp->next = t->next;
            t->next = tmp;
            printf("New: OK
    ");
        }
        else
        {
            printf("ERROR: Exist
    ");
        }
    }
    void Login(LL id,char pw[],Hashlist H)
    {
        List f = Find(id,pw,H);
        if(f==NULL)
        {
            printf("ERROR: Not Exist
    ");
        }
        else
        {
            if(strcmp(pw,f->password))
                printf("ERROR: Wrong PW
    ");
            else
                printf("Login: OK
    ");
        }
    }
    int main()
    {
        int n;
        LL tmp;
        char flag,pw[MAXN];
        scanf("%d",&n);
        Hashlist H = Init(NextPrim(n));
        for(int i=0;i<n;i++)
        {
            getchar();
            scanf("%c%lld%s",&flag,&tmp,pw);
            if(flag == 'N')
                Insert(tmp,pw,H);
            else
                Login(tmp,pw,H);
        }
    }
  • 相关阅读:
    基本数据结构:链表(list)
    字符串函数
    TCHAR
    开源库链接
    视频 链接
    tabbar
    加密
    安全类链接,https
    资本
    审核 -链接 - 发布证书
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6628506.html
Copyright © 2020-2023  润新知