QQ帐户的申请与登陆

QQ帐户的申请与登陆

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

代码如下(非散列)

#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main(){
	map<string,int> qq; // 储存qq密码在vector里的位置
	vector<string> vi(1); // 储存qq密码
	string num,password;
	char state;
	int N,tag=1; cin>>N;
	while(N--){
		cin>>state>>num>>password;
		if(state=='L'){
			if(qq[num]==0) printf("ERROR: Not Exist
");
			else if(vi[qq[num]]==password)
			printf("Login: OK
");
			else 
			printf("ERROR: Wrong PW
");
		}
		else{
			if(qq[num]!=0)
			printf("ERROR: Exist
");
			else{
				qq[num]=tag++;
				vi.push_back(password);
				printf("New: OK
");
			}
		}
	} 
	return 0; 
} 

代码如下(散列)

#include<iostream>
#include<string.h> 
#include<math.h>
using namespace std;
char state;
typedef struct node* list;
struct node{
	char qq[11];
	char password[17];
	list next; 
};
typedef struct hashtable*  hashtbl;
struct hashtable{
	int tblsize;
	list heads;
};
int nextprime(int N){
	N=N%2?N+2:N+1;
	int i;
	while(1){
		for(i=sqrt(N);i>2;i--)
			if(!(N%i)) break;
			if(i==2) break;
			else N=N+2;
	}
	return N;
}
hashtbl createtbl(int N){
	hashtbl h=new hashtable();
	h->tblsize=nextprime(N);
	h->heads=new node[h->tblsize];
	for(int i=0;i<h->tblsize;i++)
	h->heads[i].next=NULL;
	return h;
}
int Hash(int m,int tblsize){
	return m%tblsize; 
}
list find(hashtbl h,char num[11]){
	int pos=Hash(atoi(num+4),h->tblsize);
	list p=h->heads[pos].next;
	while(p!=NULL){
		if(strcmp(p->qq,num)==0) break;
		p=p->next;
	}
	return p;
}

bool Insert(hashtbl h,char num[11],char password[17]){
	list p=find(h,num);
	if(p!=NULL) {
		if(state=='N')
		printf("ERROR: Exist
");
		else if(strcmp(p->password,password)==0)
		printf("Login: OK
");
		else
		printf("ERROR: Wrong PW
");
		return false;	
	}
	else {
		if(state=='N')
		printf("New: OK
");
		else{
		printf("ERROR: Not Exist
");
		return false;	
		}
		int pos=Hash(atoi(num+4),h->tblsize);
		p=new node();
		strcpy(p->password,password);
		strcpy(p->qq,num);
		p->next=h->heads[pos].next;
		h->heads[pos].next=p;
		return true;
	}
}
int main(){
	int N; 
	scanf("%d",&N);
	hashtbl h=createtbl(N);
        char num[11],password[17];
	for(int i=0;i<N;i++){
	        getchar();	
		scanf("%c %s %s",&state,num,password); 
		Insert(h,num,password);
	}
	return 0; 
} 
原文地址:https://www.cnblogs.com/A-Little-Nut/p/8166116.html