問題描述
實現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 [email protected]
N 1234567890 [email protected]
N 1234567890 [email protected]
L 1234567890 myQQ@qq
L 1234567890 [email protected]
輸出樣例:
ERROR: Not Exist
New: OK
ERROR: Exist
ERROR: Wrong PW
Login: OK
代碼實現
#include<bits/stdc++.h>
using namespace std;
/*****************************************
map的key對應QQ,value對應password。
count()函數用來檢測QQ賬號是否存在。
find()函數在QQ存在的情況下,用來尋找QQ對應的密碼。
******************************************/
int main()
{
map<string,string> id;
int n;
char op;
string qq, pwd;
scanf("%d", &n);
for(int i=0; i<n; ++i) {
getchar(); //吃掉回車
scanf("%c", &op);
if(op == 'N') { //新建
cin >> qq >> pwd;
if(id.count(qq)) { //賬號已存在
printf("ERROR: Exist\n");
}
else {
id.insert(pair<string,string>(qq, pwd));
printf("New: OK\n");
}
}
else { //登錄
cin >> qq >> pwd;
if(!id.count(qq)) { //賬號不存在
printf("ERROR: Not Exist\n");
}
else { //賬號存在
map<string,string>::iterator it;
it = id.find(qq);
if(it->second == pwd) //密碼正確
printf("Login: OK\n");
else //密碼錯誤
printf("ERROR: Wrong PW\n");
}
}
}
return 0;
}