码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-27 22:03:10      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:thml   malloc   name   err   efi   字符串   ott   大于   简化   

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

输入格式:

输入首先给出一个正整数NN(\le 10^510?5??),随后给出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\n");
    }
    else
    {
        printf("ERROR: Exist\n");
    }
}
void Login(LL id,char pw[],Hashlist H)
{
    List f = Find(id,pw,H);
    if(f==NULL)
    {
        printf("ERROR: Not Exist\n");
    }
    else
    {
        if(strcmp(pw,f->password))
            printf("ERROR: Wrong PW\n");
        else
            printf("Login: OK\n");
    }
}
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);
    }
}

 

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

标签:thml   malloc   name   err   efi   字符串   ott   大于   简化   

原文地址:http://www.cnblogs.com/joeylee97/p/6628506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!