我的Data Structures and Algorithms代码仓:https://github.com/617076674/Data-Structures-and-Algorithms
原题链接:https://pintia.cn/problem-sets/16/problems/689
题目描述:
知识点:map集合的应用
思路:用一个map存储账户信息,键为QQ号码,对应的值为密码
用long long型变量读取QQ号码,用char数组存储字符串信息。
注意char数组的存储方式,需要用strcmp函数拷贝一份存储进map集合里,不能直接存储其地址Password。
C++代码:
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
int N;
char Command[2];
long long QQ_No;
map<long long, char[17]> accounts; //注意字符串的存储方式
int main(){
scanf("%d", &N);
for(int i = 0; i < N; i++){
char Password[17];
scanf("%s %lld %s", Command, &QQ_No, Password);
if(Command[0] == 'R'){
if(accounts.find(QQ_No) != accounts.end()){
printf("ERROR: Account Number Already Exists\n");
}else{
printf("Register Successful\n");
strcpy(accounts[QQ_No], Password); //将Password复制一份进accounts[QQ_No]
}
}else if(Command[0] == 'L'){
if(accounts.find(QQ_No) != accounts.end()){
if(strcmp(accounts[QQ_No], Password) == 0){
printf("Log in Successful\n");
}else{
printf("ERROR: Wrong Password\n");
}
}else{
printf("ERROR: Account Not Exist\n");
}
}
}
return 0;
}
C++解题报告: