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

11-散列3 QQ帐户的申请与登陆


实现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

code

# include <iostream>
# include <cstdio>
# include <cstring>

struct Entry {
	long long account;
	char password[20];
};

struct Hash {
	int n;
	Entry * tableList;
	Hash(int _n) :n(_n)
	{
		tableList = new Entry[n];
		for (int i = 0; i < n; ++i) tableList[i].account = -1;
	}
	int find(long long val)
	{
		int i;
		for (i = val % n; tableList[i].account != -1 && tableList[i].account != val; i = (i + 1) % n);
		return i;
	}
};

struct QQ {
	Hash ht;
	QQ() : ht(333331){}
	void NewAccount()
	{
		long long ac;
		char password[20];

		scanf("%lld%s\n", &ac, password);

		int pos = ht.find(ac);
		if (ht.tableList[pos].account == -1)
		{
			ht.tableList[pos].account = ac;
			strcpy(ht.tableList[pos].password, password);
			printf("New: OK\n");
		}
		else
		{
			printf("ERROR: Exist\n");
		}

	}

	void LoginAccount()
	{
		long long ac;
		char password[20];

		scanf("%lld%s\n", &ac, password);

		int pos = ht.find(ac);
		if (ht.tableList[pos].account == -1)
		{
			printf("ERROR: Not Exist\n");
		}
		else
		{
			if (strcmp(password, ht.tableList[pos].password) == 0)
			{
				printf("Login: OK\n");
			}
			else
			{
				printf("ERROR: Wrong PW\n");
			}
		}
	}
};

int main(void)
{
	int n;
	scanf("%d\n", &n);
	QQ q;

	while (n--)
	{
		char op;
		scanf("%c", &op);
		
		switch (op){
			case 'N': q.NewAccount(); break;
			case 'L': q.LoginAccount(); break;
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值