基础实验5-2.3 QQ帐户的申请与登陆 (25分)和进阶实验5-3.1 航空公司VIP客户查询 (25分)
基础实验5-2.3 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
使用散列查找法。
散列函数我使用了账户的后7位字符转化为相应整数(不足7位的全部用上)。
冲突解决方案采用分离链接法。
C语言实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char * ElementType;
struct node
{
ElementType account;
ElementType passcode;
struct node * Next;
};
typedef struct node * Node;
struct hashtable
{
Node people;
int Tablesize;
};
typedef struct hashtable * HashTable;
int isprime(int n);
int leastprime(int n);
HashTable CreateHash(int N);
void Insert(HashTable H, ElementType acc, ElementType pas);
Node Find(HashTable H, ElementType acc);
int Hash(ElementType acc, int tablesize);
int main()
{
int N, size;
int i;
scanf("%d\n", &N);
size = leastprime(N);//散列表表长取大于N的最小素数
HashTable H;
H = CreateHash(size);
char c;
char command;
Node n;
ElementType a, p;
a = (ElementType)malloc(11 * sizeof(char));
p = (ElementType)malloc(17 * sizeof(char));
for (i = 0; i < N; i++)
{
scanf("%c", &command);//读入命令L/N
scanf("%c", &c);//读入空格
scanf("%s", a);//读入账号
scanf("%c", &c);//读入空格
scanf("%s", p);//读入密码
scanf("%c", &c);//读入换行符
if (command == 'L')
{
n = Find(H, a);
if (n == NULL) { printf("ERROR: Not Exist\n"); }//老账户QQ号码不存在
else
{
if (strcmp(n->passcode, p) != 0)
{
printf("ERROR: Wrong PW\n");//老账户密码错误
}
else
{
printf("Login: OK\n");//老账户登陆成功
}
}
}
else if (command == 'N')
{
n = Find(H, a);
if (n == NULL)
{
Insert(H, a, p);//插入新账户
printf("New: OK\n");//新申请账户成功
}
else
{
printf("ERROR: Exist\n");//新申请的号码已经存在
}
}
}
return 0;
}
int isprime(int n)//判断素数
{
int i;
if (n == 1) { return 0; }
else
{
for (i = 2; i <= sqrt(n); i++)
{
if (n%i == 0) { return 0; }
}
return 1;
}
}
int leastprime(int n)//返回大于等于n的最小素数
{
if (isprime(n)) { return n; }
else
{
while (isprime(n) == 0)
{
n++;
}
return n;
}
}
HashTable CreateHash(int size)//创建散列表
{
int i;
HashTable H;
H = (HashTable)malloc(sizeof(struct hashtable));
H->Tablesize = size;
H->people = (Node)malloc((H->Tablesize) * sizeof(struct node));
for (i = 0; i < H->Tablesize; i++)
{
H->people[i].account = (ElementType)malloc(11 * sizeof(char));
H->people[i].passcode = (ElementType)malloc(17 * sizeof(char));
H->people[i].Next = NULL;
}
return H;
}
//插入新账户,每次插在对应头结点之后
void Insert(HashTable H, ElementType acc, ElementType pas)
{
int index;
index = Hash(acc, H->Tablesize);
Node newpeople;
newpeople = (Node)malloc(sizeof(struct node));
newpeople->account = (ElementType)malloc(11 * sizeof(char));
newpeople->passcode = (ElementType)malloc(17 * sizeof(char));
strcpy(newpeople->account, acc);
strcpy(newpeople->passcode, pas);
newpeople->Next = H->people[index].Next;
H->people[index].Next = newpeople;
}
//查找账户是否存在,不存在返回NULL,存在返回结点指针
Node Find(HashTable H, ElementType acc)
{
int index;
index = Hash(acc, H->Tablesize);
Node p;
p = H->people[index].Next;
while (p != NULL)
{
if (strcmp(p->account, acc) == 0) { return p; }
p = p->Next;
}
return p;
}
//散列函数计算,使用账户后7位(不足7位全部用上)
int Hash(ElementType acc, int tablesize)
{
int len = strlen(acc);
int i;
int h = 0;
if (len < 7)
{
for (i = 0; i < len; i++)
{
h = h * 10 + acc[i];
}
}
else
{
for (i = len - 7; i < len; i++)
{
h = h * 10 + acc[i];
}
}
return h % tablesize;
}
另一道题思路基本一致,也一起写进来了。
进阶实验5-3.1 航空公司VIP客户查询 (25分)
不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务。现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快速查询会员里程积分的功能。
输入格式:
输入首先给出两个正整数N(≤10^5)和K(≤500)。其中K是最低里程,即为照顾乘坐短程航班的会员,航空公司还会将航程低于K公里的航班也按K公里累积。随后N行,每行给出一条飞行记录。飞行记录的输入格式为:18位身份证号码(空格)飞行里程。其中身份证号码由17位数字加最后一位校验码组成,校验码的取值范围为0~9和x共11个符号;飞行里程单位为公里,是(0, 15 000]区间内的整数。然后给出一个正整数M(≤10^5),随后给出M行查询人的身份证号码。
输出格式:
对每个查询人,给出其当前的里程累积值。如果该人不是会员,则输出No Info。每个查询结果占一行。
输入样例:
4 500
330106199010080419 499
110108198403100012 15000
120104195510156021 800
330106199010080419 1
4
120104195510156021
110108198403100012
330106199010080419
33010619901008041x
输出样例:
800
15000
1000
No Info
散列查找函数使用身份证后5位。
冲突解决方案使用分离链接法。
C语言实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char * ElementType;
struct node
{
ElementType id;
int distance;
struct node * Next;
};
typedef struct node * Node;
struct hashtable
{
Node vip;
int Tablesize;
};
typedef struct hashtable * HashTable;
int isprime(int n);
int leastprime(int n);
HashTable CreateHash(int size);
void Insert(HashTable H, ElementType i, int d);
Node Find(HashTable H, ElementType i);
int Hash(ElementType i, int tablesize);
int main()
{
int N, K, M, size;
int i;
scanf("%d %d\n", &N, &K);
size = leastprime(N);//表长取大于等于N的最小素数
HashTable H;
H = CreateHash(size);//创建散列表
ElementType iden;
iden = (ElementType)malloc(19 * sizeof(char));
Node t;
int dis;
for (i = 0; i < N; i++)//读入数据并插入散列表
{
scanf("%s", iden);//读入身份证号
scanf("%d\n", &dis);//读入里程数
if (dis < K) { dis = K; }//里程数低于K公里按K公里计算
Insert(H, iden, dis);
}
scanf("%d\n", &M);
char c;
for (i = 0; i < M; i++)
{
scanf("%s", iden);//读入待查询的身份证号
scanf("%c", &c);//读入换行符
t = Find(H, iden);
if (t == NULL)
{
printf("No Info\n");//不是会员
}
else
{
printf("%d\n", t->distance);//输出会员里程累积值
}
}
return 0;
}
int isprime(int n)//判断素数
{
int i;
if (n == 1) { return 0; }
else
{
for (i = 2; i <= sqrt(n); i++)
{
if (n%i == 0) { return 0; }
}
return 1;
}
}
int leastprime(int n)//返回大于等于n的最小素数
{
if (isprime(n)) { return n; }
else
{
while (isprime(n) == 0)
{
n++;
}
return n;
}
}
HashTable CreateHash(int size)//创建散列表
{
int i;
HashTable H;
H = (HashTable)malloc(sizeof(struct hashtable));
H->Tablesize = size;
H->vip = (Node)malloc((H->Tablesize) * sizeof(struct node));
for (i = 0; i < H->Tablesize; i++)
{
H->vip[i].id = (ElementType)malloc(19 * sizeof(char));
H->vip[i].Next = NULL;
H->vip[i].distance = 0;
}
return H;
}
//如果账户不存在,插入新账户,每次插在对应头结点之后
//如果账户已经存在,里程数累积值增加
void Insert(HashTable H, ElementType i, int d)
{
int index;
index = Hash(i, H->Tablesize);
Node p = Find(H, i);
if (p == NULL)
{
Node newpeople;
newpeople = (Node)malloc(sizeof(struct node));
newpeople->id = (ElementType)malloc(19 * sizeof(char));
strcpy(newpeople->id, i);
newpeople->distance = d;
newpeople->Next = H->vip[index].Next;
H->vip[index].Next = newpeople;
}
else
{
p->distance = p->distance + d;
}
}
//查找账户是否存在,不存在返回NULL,存在返回结点指针
Node Find(HashTable H, ElementType i)
{
int index;
index = Hash(i, H->Tablesize);
Node p;
p = H->vip[index].Next;
while (p != NULL)
{
if (strcmp(p->id, i) == 0) { return p; }
p = p->Next;
}
return p;
}
//散列函数计算,取身份证号后5位
int Hash(ElementType i, int tablesize)
{
int j;
int h = 0;
for (j = 13; j < 17; j++)
{
h = h * 10 + i[j] - '0';
}
if (i[j] >= '0'&&i[j] <= '9')
{
h = h * 10 + i[j] - '0';
}
else if (i[j] == 'x')
{
h = h * 10 + 10;
}
return h % tablesize;
}