题目描述
实现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
题解
题意理解:根据要求完成 QQ 号的创建和登陆操作
解题思路与 11-散列1 电话聊天狂人 类似,建哈希表、查找、插入操作只需做略微修改即可。
但是本题注意一下哈希函数传入时取位,由于最短的 QQ 号为 4 位数,使用 atoi() 函数时取 qqnumber + 4 。
这样就保证了即使是 4 位的 QQ 号,他也会被取到最后一位来进行哈希函数计算。
如果大于 4 就会出现溢出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
typedef struct LNode *PtrToLNode;
struct LNode {
char ID[20];
char PW[20];
PtrToLNode Next;
};
typedef struct TblNode *HashTable;
struct TblNode {
int TableSize;
PtrToLNode Head;
};
int NextPrime(int N)
{
int i, p;
if (N == 1) return 2;
p = (N % 2) ? N + 2 : N + 1;
while (p <= 1000000) {
for (i = (int)sqrt(p); i > 2; i--)
if (!(p % i)) break;
if (i == 2) break;
else p += 2;
}
return p;
}
HashTable CreateTable(int TableSize)
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = NextPrime(TableSize);
H->Head = (PtrToLNode)malloc(sizeof(struct LNode) * H->TableSize);
for(i = 0; i < H->TableSize; i++) {
H->Head[i].ID[0] = '\0';
H->Head[i].PW[0] = '\0';
H->Head[i].Next = NULL;
}
return H;
}
int Hash(int key, int p)
{
return key % p;
}
PtrToLNode Find(HashTable H, char qqnumber[])
{
PtrToLNode Ptr;
int Pos;
Pos = Hash(atoi(qqnumber + 4), H->TableSize);
Ptr = H->Head[Pos].Next;
while (Ptr) {
if (strcmp(Ptr->ID, qqnumber) == 0) break;
Ptr = Ptr->Next;
}
return Ptr;
}
bool Insert(HashTable H, char qqnumber[], char password[])
{
PtrToLNode Ptr, NewCell;
int Pos;
Pos = Hash(atoi(qqnumber + 4), H->TableSize);
Ptr = Find(H, qqnumber);
if (!Ptr) {
NewCell = (PtrToLNode)malloc(sizeof(struct LNode));
strcpy(NewCell->ID, qqnumber);
strcpy(NewCell->PW, password);
NewCell->Next = H->Head[Pos].Next;
H->Head[Pos].Next = NewCell;
return true;
} else {
return false;
}
}
void FreeTable(HashTable H)
{
PtrToLNode p, tmp;
for (int i = 0; i < H->TableSize; i++) {
p = H->Head[i].Next;
while (p) {
tmp = p;
p = p->Next;
free(tmp);
}
}
free(H->Head);
free(H);
}
int main()
{
int i, N;
char cmd, QQNumber[20], PassWord[20];
HashTable H;
PtrToLNode Ptr;
scanf("%d", &N); getchar();
H = CreateTable(N);
for (i = 0; i < N; i++) {
scanf("%c", &cmd); getchar();
scanf("%s", QQNumber); getchar();
scanf("%s", PassWord); getchar();
Ptr = Find(H, QQNumber);
switch (cmd) {
case 'N':
if (!Ptr) {
Insert(H, QQNumber, PassWord);
printf("New: OK\n");
} else {
printf("ERROR: Exist\n");
}
break;
case 'L':
if (Ptr) {
if (strcmp(Ptr->PW, PassWord) == 0) printf("Login: OK\n");
else printf("ERROR: Wrong PW\n");
} else {
printf("ERROR: Not Exist\n");
}
break;
}
}
FreeTable(H);
return 0;
}