5-15 QQ帐户的申请与登陆 (25分)
实现QQ新帐户申请和老帐户登陆的简化版功能。最大挑战是:据说现在的QQ号码已经有10位数了。
输入格式:
输入首先给出一个正整数NN(\le 10^5≤105),随后给出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
真的心累。一个散列表,debug了好久,结果发现把string改成char【】就可以了,到现在还不明白为什么会这样。如果有大佬知道希望能指点我一下。然后还用了一个另一种map的方法。
#include<iostream> //map做
#include<cstdio>
#include<map>
#include<string>
using namespace std;
int main()
{
int n;
map<string,string> m;
cin>>n;
for(int i=0;i<n;i++)
{
char temp;
cin>>temp;
string a,b;
cin>>a>>b;
if(temp=='L')
{
if(!m.count(a))
{
cout<<"ERROR: Not Exist"<<endl;
}
else
{
if(m[a]!=b)
{
cout<<"ERROR: Wrong PW"<<endl;
}
else
{
cout<<"Login: OK"<<endl;
}
}
}
if(temp=='N')
{
if(m.count(a))
{
cout<<"ERROR: Exist"<<endl;
}
else
{
m.insert(pair<string,string>(a,b));
cout<<"New: OK"<<endl;
}
}
}
return 0;
}
#include<iostream> //散列表
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef struct node
{
long long id;
char ps[17];
struct node* next;
}*List;
typedef struct tb
{
long long size;
List* list;
}*HashTable;
int Hash(long long id,long long size)
{
return id%size;
}
int NextPrim(int x)
{
int j;
for(int t=x;;t++)
{
for(j=2;j*j<=t;j++)
if(t%j==0)
break;
if(j*j>t)
return t;
}
}
HashTable Create(long long num)
{
HashTable hs=(HashTable)malloc(sizeof(struct tb));
hs->size=num;
hs->list=(List*)malloc(sizeof(List)*hs->size);
for(int i=0;i<hs->size;i++)
{
hs->list[i]=(List)malloc(sizeof(struct node));
hs->list[i]->next=NULL;
}
return hs;
}
List find(long long num,char x[],HashTable h)
{
List L=h->list[Hash(num,h->size)];
List LL=L->next;
while(LL!=NULL&&LL->id!=num)
{
LL=LL->next;
}
return LL;
}
void Insert(long long num,char x[],HashTable h)
{
List L=find(num,x,h);
if(L==NULL)
{
List t=h->list[Hash(num,h->size)];
List temp=(List)malloc(sizeof(struct node));
temp->id=num;
// temp->ps=x;
strcpy(temp->ps,x);
temp->next=t->next;
t->next=temp;
cout<<"New: OK"<<endl;
}
else
{
cout<<"ERROR: Exist"<<endl;
}
}
void login(long long num,char x[],HashTable h)
{
List L=find(num,x,h);
if(L==NULL)
{
cout<<"ERROR: Not Exist"<<endl;
}
else
{
if(!strcmp(L->ps,x))
{
cout<<"Login: OK"<<endl;
}
else
{
cout<<"ERROR: Wrong PW"<<endl;
}
}
}
int main()
{
int n;
cin>>n;
HashTable h=Create(NextPrim(n));
for(int i=0;i<n;i++)
{
getchar();
long long t;
char s[17];
char c;
cin>>c>>t;
scanf("%s",s);
if(c=='L')
{
login(t,s,h);
}
else
{
Insert(t,s,h);
}
}
return 0;
}