PTA数据结构与算法题目集(中文) 7-15

PTA数据结构与算法题目集(中文)  7-15

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

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

输入格式:

输入首先给出一个正整数N(≤),随后给出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
题目分析:一道散列的基本题 读入后进行判断
  1 #define _CRT_SECURE_NO_WARNINGS   
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<malloc.h>
  5 #include<math.h>
  6 #include<string.h>
  7 #define KEYLENGTH 16
  8 #define MAXTABLESIZE 10000
  9 
 10 typedef char ElementType[KEYLENGTH + 1];
 11 typedef struct LNode* PtrToLNode;
 12 typedef PtrToLNode List;
 13 struct LNode
 14 {
 15     ElementType QNumber;
 16     ElementType QPassword;
 17     PtrToLNode Next;
 18 };
 19 typedef struct HblNode* HashTable;
 20 struct HblNode
 21 {
 22     int TableSize;
 23     List Heads;
 24 };
 25 
 26 int NextPrime(int num)
 27 {
 28     int p = (num % 2) ? (num + 2) : (num + 1);
 29     int i = 0;
 30     while (p<MAXTABLESIZE)
 31     {
 32         for (i = (int)sqrt(p); i > 2; i--)
 33             if (p % i == 0)break;
 34         if (i == 2)break;
 35         else
 36             p += 2;
 37     }
 38     return p;
 39 }
 40 
 41 int Hash(ElementType QNumber, int TabelSize)
 42 {
 43     return atoi(QNumber + 3) % TabelSize;
 44 }
 45 
 46 HashTable BuildHashTable(int TabelSize)
 47 {
 48     HashTable H = (HashTable)malloc(sizeof(struct HblNode));
 49     H->TableSize = NextPrime(TabelSize);
 50     H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
 51     for (int i = 0; i < H->TableSize; i++)
 52     {
 53         H->Heads[i].QNumber[0] = '\0';
 54         H->Heads[i].QPassword[0] = '\0';
 55         H->Heads[i].Next = NULL;
 56     }
 57     return H;
 58 }
 59 
 60 PtrToLNode Find(ElementType Qnumber, HashTable H)
 61 {
 62     int i = Hash(Qnumber,H->TableSize);
 63     PtrToLNode P = H->Heads[i].Next;
 64     while (P && strcmp(P->QNumber, Qnumber))
 65         P = P->Next;
 66     return P;
 67 }
 68 void Insert(ElementType QNumber,ElementType QPassword,HashTable H)
 69 {
 70     PtrToLNode P, NewCell;
 71     P = Find(QNumber, H);
 72     if (!P)
 73     {
 74         int i = Hash(QNumber, H->TableSize);
 75         NewCell = (PtrToLNode)malloc(sizeof(struct LNode));
 76         strcpy(NewCell->QNumber, QNumber);
 77         strcpy(NewCell->QPassword, QPassword);
 78         NewCell->Next = H->Heads[i].Next;
 79         H->Heads[i].Next = NewCell;
 80     }
 81 }
 82 
 83 void Order_L(HashTable H)
 84 {
 85     ElementType QNumber;
 86     ElementType QPassword;
 87     scanf("%s %s", QNumber, QPassword);
 88     PtrToLNode P = Find(QNumber, H);
 89     if (P)
 90     {
 91         if (!strcmp(P->QPassword, QPassword))
 92             printf("Login: OK\n");
 93         else
 94             printf("ERROR: Wrong PW\n");
 95     }
 96     else
 97         printf("ERROR: Not Exist\n");
 98 }
 99 void Order_N(HashTable H)
100 {
101     ElementType QNumber;
102     ElementType QPassword;
103     scanf("%s %s", QNumber, QPassword);
104     PtrToLNode P = Find(QNumber, H);
105     if (P)
106         printf("ERROR: Exist\n");
107     else
108     {
109         Insert(QNumber, QPassword, H);
110         printf("New: OK\n");
111     }
112 }
113 void CreateHashTable()
114 {
115     int N;
116     scanf("%d\n", &N);
117     HashTable H = BuildHashTable(N);
118     while (N--)
119     {
120         char Order[2];
121         scanf("%s", Order);
122         switch (Order[0])
123         {
124         case 'L':Order_L(H); break;
125         case 'N':Order_N(H); break;
126         }
127     }
128 }
129 
130 int main()
131 {
132     CreateHashTable();
133     return 0;
134 }
View Code

猜你喜欢

转载自www.cnblogs.com/57one/p/11602090.html