实现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
这题比较简单,直接用散列,散列实现的代码来自MOOC,应该批评
/*QQ帐户的申请与登陆*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAXTABLESIZE 100000 /* 允许开辟的最大散列表长度 */
typedef char* ElementType; /* 关键词类型用整型 */
typedef int Index; /* 散列地址类型 */
typedef Index Position; /* 数据所在位置与散列地址是同一类型 */
/* 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素 */
typedef enum { Legitimate, Empty, Deleted } EntryType;
typedef struct HashEntry Cell; /* 散列表单元类型 */
struct HashEntry {
char Data[10]; /* 存放元素 */
char Password[17];
EntryType Info; /* 单元状态 */
};
typedef struct TblNode* HashTable; /* 散列表类型 */
struct TblNode { /* 散列表结点定义 */
int TableSize; /* 表的最大长度 */
Cell* Cells; /* 存放散列单元数据的数组 */
};
Index Hash(const char* Key, int TableSize) {
unsigned int h = 0; //散列函数值,初始化为0
while (*Key != '\0') //位移映射
h = (h << 5) + *Key++;
return h % TableSize;
}
int NextPrime(int N)
{ /* 返回大于N且不超过MAXTABLESIZE的最小素数 */
int i, p = (N % 2) ? N + 2 : N + 1; /*从大于N的下一个奇数开始 */
while (p <= MAXTABLESIZE) {
for (i = (int)sqrt(p); i > 2; i--)
if (!(p % i)) break; /* p不是素数 */
if (i == 2) break; /* for正常结束,说明p是素数 */
else p += 2; /* 否则试探下一个奇数 */
}
return p;
}
HashTable CreateTable(int TableSize)
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct TblNode));
/* 保证散列表最大长度是素数 */
H->TableSize = NextPrime(TableSize);
/* 声明单元数组 */
H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
/* 初始化单元状态为“空单元” */
for (i = 0; i < H->TableSize; i++)
H->Cells[i].Info = Empty;
return H;
}
Position Find(HashTable H, ElementType Key)
{
Position CurrentPos, NewPos;
int CNum = 0; /* 记录冲突次数 */
NewPos = CurrentPos = Hash(Key, H->TableSize); /* 初始散列位置 */
/* 当该位置的单元非空,并且不是要找的元素时,发生冲突 */
while (H->Cells[NewPos].Info != Empty && strcmp(H->Cells[NewPos].Data,Key)!=0) {
/* 字符串类型的关键词需要 strcmp 函数!! */
/* 统计1次冲突,并判断奇偶次 */
if (++CNum % 2) { /* 奇数次冲突 */
NewPos = CurrentPos + (CNum + 1) * (CNum + 1) / 4; /* 增量为+[(CNum+1)/2]^2 */
if (NewPos >= H->TableSize)
NewPos = NewPos % H->TableSize; /* 调整为合法地址 */
}
else { /* 偶数次冲突 */
NewPos = CurrentPos - CNum * CNum / 4; /* 增量为-(CNum/2)^2 */
while (NewPos < 0)
NewPos += H->TableSize; /* 调整为合法地址 */
}
}
return NewPos; /* 此时NewPos或者是Key的位置,或者是一个空单元的位置(表示找不到)*/
}
int Insert(HashTable H, ElementType Key,ElementType pass)
{
Position Pos = Find(H, Key); /* 先检查Key是否已经存在 */
if (H->Cells[Pos].Info != Legitimate) { /* 如果这个单元没有被占,说明Key可以插入在此 */
H->Cells[Pos].Info = Legitimate;
strcpy(H->Cells[Pos].Data, Key);
strcpy(H->Cells[Pos].Password, pass);
// printf("%s %s\n", H->Cells[Pos].Data, H->Cells[Pos].Password);
/*字符串类型的关键词需要 strcpy 函数!! */
return 1;
}
else {
return 0;
}
}
int main() {
int N;
char laji;
scanf("%d", &N);
char F;
char a[11];
char pass[17];
int k;
HashTable H = CreateTable(N);
for (int i = 0; i < N; i++) {
scanf("%c", &laji);
scanf("%c %s %s", &F, a, pass);
if (F == 'N') {
k =Insert(H, a, pass);
if (k == 0) {
printf("ERROR: Exist\n");
}
else printf("New: OK\n");
}
else {
k = Find(H, a);
if (H->Cells[k].Info == Empty) {
printf("ERROR: Not Exist\n");
}
else {
if (strcmp(H->Cells[k].Password, pass) == 0) {
printf("Login: OK\n");
}
else {
printf("ERROR: Wrong PW\n");
}
}
}
}
return 0;
}