实现QQ新帐户申请和老帐户登陆的简化版功能。最大挑战是:据说现在的QQ号码已经有10位数了。
这是一道哈希表的问题,使用map解决会更为方便:
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
map<string, string> mp;
void Login(string id, string pw){
if(mp[id] == ""){
cout << "ERROR: Not Exist" << endl;
}else if(pw != mp[id])
cout << "ERROR: Wrong PW" << endl;
else cout << "Login: OK" << endl;
}
void Register(string id, string pw){
if(mp[id] != "")
cout << "ERROR: Exist" << endl;
else{
mp[id] = pw;
cout << "New: OK" << endl;
}
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
char action;
string id, password;
cin >> action >> id >> password;
switch(action){
case 'L' : Login(id, password); break;
case 'N' : Register(id, password); break;
}
}
return 0;
}