7-21 QQ帳戶的申請與登陸 (25分)

實現QQ新帳戶申請和老帳戶登陸的簡化版功能。最大挑戰是:據說現在的QQ號碼已經有10位數了。

輸入格式:

輸入首先給出一個正整數N(≤10​5​​),隨後給出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 [email protected]
N 1234567890 [email protected]
N 1234567890 [email protected]
L 1234567890 myQQ@qq
L 1234567890 [email protected]

輸出樣例:

ERROR: Not Exist
New: OK
ERROR: Exist
ERROR: Wrong PW
Login: OK
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 110017
typedef struct UserNode* ptrUser;
struct UserNode{
    int ID;
    char password[20];
    ptrUser next;
};
 
ptrUser HashTable[MAXN];
 
void InsertUserNode(int idx,int id,char* pwd)
{
    ptrUser P;
    P=malloc(sizeof(struct UserNode));
    P->ID=id;
    strcpy(P->password,pwd);
    P->next=HashTable[idx];
    HashTable[idx]=P;
}
int Hash(int x)
{
    return x%MAXN;
}
ptrUser FindUser(int id)
{
    ptrUser P;
    int idx=Hash(id);
    P=HashTable[idx];
    while(P!=NULL)
    {
        if (P->ID==id)
        return P;
        P=P->next;
    }
    return NULL;
}
 
int FindNextPrime(int n) //找個比n大的質數
{
    if(n==1)  //在這裏有個坑,第二個測試點卡表大小填1的時候。
        return 2;
    int i,isPrime;
    while(1)
    {
        isPrime=1;
        for(i=2;i<n;i++)
            if(n%i==0)
            {
                isPrime=0;
                break;
            }
        if(isPrime)
            return n;
        else n++;
    }
}
void Login(int id,char pwd[])
{
    ptrUser P;
    P=FindUser(id);
    if(P==NULL)
    {
        printf("ERROR: Not Exist\n");
        return;
    }
    else if(strcmp(P->password,pwd)==0)
    {
        printf("Login: OK\n");
    }
    else
        printf("ERROR: Wrong PW\n");
         
}
 
void NewUser(int id,char pwd[])
{
    ptrUser P;
    P=FindUser(id);
    if(P!=NULL)
    {
        printf("ERROR: Exist\n");
        return;
    }
    else
    {
        InsertUserNode(Hash(id),id,pwd);
        printf("New: OK\n");
    }
}
 
int main()
{
    int i,N;
    char command[3];
    int id;
    char pwd[20];
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",command);
        scanf("%d",&id);
        scanf("%s",pwd);
        if(command[0]=='L')
            Login(id,pwd);
        else if(command[0]=='N')
            NewUser(id,pwd);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章