实现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
成功代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 110017
typedef struct UserNode* ptrUser;
struct UserNode{
int ID;
char password[20];
ptrUser next;
};
ptrUser HashTable[MAXN];
void InsertUserNode(int idx,int id,char* pwd)
{
ptrUser P;
P=malloc(sizeof(struct UserNode));
P->ID=id;
strcpy(P->password,pwd);
P->next=HashTable[idx];
HashTable[idx]=P;
}
int Hash(int x)
{
return x%MAXN;
}
ptrUser FindUser(int id)
{
ptrUser P;
int idx=Hash(id);
P=HashTable[idx];
while(P!=NULL)
{
if (P->ID==id)
return P;
P=P->next;
}
return NULL;
}
int FindNextPrime(int n) //找个比n大的质数
{
if(n==1) //在这里有个坑,第二个测试点卡表大小填1的时候。
return 2;
int i,isPrime;
while(1)
{
isPrime=1;
for(i=2;i<n;i++)
if(n%i==0)
{
isPrime=0;
break;
}
if(isPrime)
return n;
else n++;
}
}
void Login(int id,char pwd[])
{
ptrUser P;
P=FindUser(id);
if(P==NULL)
{
printf("ERROR: Not Exist\n");
return;
}
else if(strcmp(P->password,pwd)==0)
{
printf("Login: OK\n");
}
else
printf("ERROR: Wrong PW\n");
}
void NewUser(int id,char pwd[])
{
ptrUser P;
P=FindUser(id);
if(P!=NULL)
{
printf("ERROR: Exist\n");
return;
}
else
{
InsertUserNode(Hash(id),id,pwd);
printf("New: OK\n");
}
}
int main()
{
int i,N;
char command[3];
int id;
char pwd[20];
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s",command);
scanf("%d",&id);
scanf("%s",pwd);
if(command[0]=='L')
Login(id,pwd);
else if(command[0]=='N')
NewUser(id,pwd);
}
}