11-散列3 QQ帐户的申请与登陆   (25分)

9 篇文章 0 订阅
4 篇文章 0 订阅

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

输入格式:

输入首先给出一个正整数NN10^5),随后给出NN行指令。每行指令的格式为:“命令符(空格)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账号和密码(与电话聊天狂人解法相近)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define KEYLENGTH 10
#define MAXTABLESIZE 100000
typedef char ElementType[KEYLENGTH + 1];
typedef char PasswordType[17];
typedef int Index;

typedef struct LNode *PtrToLNode;
struct LNode {
	ElementType Data;
	PtrToLNode Next;
	PasswordType Pw;	//增加密码
};
typedef struct LNode *List;
typedef struct LNode *Position;

typedef struct TblNode *HashTable;
struct TblNode {
	int TableSize;
	List Heads;
};

int NextPrime ( int n ) {
	if ( n == 1 ) 
		return 2;
	else {
		int i, p = n % 2 ? n + 2 : n + 1;
		while ( p <= MAXTABLESIZE ) {
			double q = p;
			for ( i = sqrt(q); i > 2; i-- )
				if ( p % i ) break;
			if ( i == 2 ) break;
			else p += 2;
		}
		return p;
	}
}

HashTable CreateTable ( int TableSize ) {
	HashTable H;
	H = (HashTable)malloc(sizeof(struct TblNode));
	H->TableSize = NextPrime( TableSize );
	H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
	for ( int i = 0; i < H->TableSize; i++ ) {
		H->Heads[i].Data[0] = '\0';
		H->Heads[i].Pw[0] = '\0';  //初始化
		H->Heads[i].Next = NULL;
	}
	return H;
}

Index Hash ( const char *key, int TableSize ) {
	unsigned int h = 0;
	while ( *key != '\0' ) {
		h = ( h << 5 ) + *key++;
	}
	return h % TableSize;
}

Position Find ( HashTable H, ElementType Key ) {
	Position P;
	Index pos;
	pos = Hash( Key + 3, H->TableSize );
	P = H->Heads[pos].Next;
	while ( P && strcmp( P->Data, Key ) ) 
		P = P->Next;
	return P;
}

bool Insert ( HashTable H, ElementType Key, PasswordType Pw) {
	Index pos;
	if ( Find( H, Key ) ) 
		return false;
	else {
		Position NewCell = (Position)malloc(sizeof(struct LNode));
		strcpy( NewCell->Data, Key );
		strcpy( NewCell->Pw, Pw );  //记录密码
		pos = Hash( Key + 3, H->TableSize );
		NewCell->Next = H->Heads[pos].Next;
		H->Heads[pos].Next = NewCell;
		return true;
	}
}

void DestroyTable ( HashTable H ) {
	int i;
	Position Tmp, P;
	for ( i = 0; i < H->TableSize; i++ ) {
		P = H->Heads[i].Next;
		while ( P != NULL ) {
			Tmp = P;
			P = P->Next;
			free(Tmp);
		}
	}
	free( H->Heads );
	free( H );
}

//检查密码是否正确
bool CheckPassword ( Position P, char pw[] ) {
	if ( strcmp( pw, P->Pw ) )
		return false;
	else
		return true;
}

//检查账户是否存在
bool FindAccount ( HashTable H, char qq[] ) {
	Position P;
	Index pos;
	pos = Hash( qq + 3, H->TableSize );
	P = H->Heads[pos].Next;
	while ( P != NULL ) {
		if ( strcmp( P->Data, qq ) == 0 )
			return 1;
		P = P->Next;
	}
	return 0;
}

int main () {
	int N;
	char c, qq[11], pw[17];
	HashTable H;
	scanf("%d", &N);
	getchar();
	H = CreateTable( 2 * N );
	while ( N-- ) {
		scanf("%c %s %s", &c, qq, pw);
		getchar();
		if ( c == 'L' ) {
			if ( FindAccount( H, qq ) ) {
				if ( CheckPassword( Find( H, qq ), pw ) )
					printf("Login: OK\n");
				else 
					printf("ERROR: Wrong PW\n");
			}
			else 
				printf("ERROR: Not Exist\n");
		}
		else {
			if ( FindAccount( H, qq) ) 
				printf("ERROR: Exist\n");
			else {
				printf("New: OK\n");
				Insert( H, qq, pw );
			}
		}
	}
	DestroyTable( H );
	system("pause");
	return 0;
}

解法二:排序,利用map容器

#include <cstdio>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;
int main () {
	int N;
	char c, s1[11], s2[17];
	string qq, pw;
	map<string, string> m;
	scanf("%d", &N);
	getchar();
	while ( N-- ) {
		scanf("%c %s %s", &c, s1, s2);
		getchar();
		qq = s1; pw = s2;
		if ( c == 'N') {
			if ( m.find(qq) != m.end() )
				printf("ERROR: Exist\n");
			else {
				printf("New: OK\n");
				m[qq] = pw;
			}
		}
		else {
			if ( m.find(qq) == m.end() )
				printf("ERROR: Not Exist\n");
			else if ( m[qq] == pw )
				printf("Login: OK\n");
			else 
				printf("ERROR: Wrong PW\n");
		}
	}
	system("pause");
	return 0;
}




  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值