Timus 2002. Test Task 一个登陆系统
2014-04-28 15:04
239 查看
It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the windows with quiet bangs. Ilya was sitting at the computer and gloomy
looking out of the window at the bleak scenery. Suddenly, a note caught his attention from the lower right corner of the screen. It said: “You have 1 unread email message(s)”. The boy braced himself for a bit of useless spam and opened the letter. But it turned
out much more interesting...
Dear Sir, You have received this message from the “Rutnok BKS” HR department!
We have received your application for a software developer and we found your CV quite interesting. We would like to suggest a simple test task to evaluate your professional skills. Your task is to implement
the register system for a forum. It must support three types of operations:
“register username password”: to register a new user with name “username” and password “password”. If such user already exists in the database, the system should output the error message “fail: user already exists”. Otherwise, it should output message “success:
new user added”.
“login username password”: to log into the system as user “username” with password “password”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user enters an incorrect password, the system should output “fail:
incorrect password”. Otherwise, if the user is already logged in the system at that moment, it should output “fail: already logged in”. Otherwise, it should output “success: user logged in”.
“logout username”: to log out of the system as user “username”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user isn’t in the system at that moment, it should output “fail: already logged out”. Otherwise,
it should output “success: user logged out”.
Use this letter as a formal description of the algorithm and follow the described format of system messages. Good luck!
Ilya stopped doing anything else and started solving the test task. You can try to solve it as well!
The first line contains an integer n that is the number of operations (1 ≤ n ≤ 100). Each of the next n lines contains a single query in the format given above. We can assume
that “username” and “password” are non-empty strings with length of up to 30 characters. All characters in these strings have codes from 33 to 126.
For each operation, print the message on a single line in the format given above. Be sure to put spaces and punctuation marks in the right places in the messages.
实现一个登陆系统。
思路:
1 使用map,在map中的就是已经注册的了
2 使用数据结构保存用户名,是否登陆和密码
3 使用if else判断处理第一个字符串-命令
类似很多人都写的什么图书馆管理系统,什么信息系统之类的登陆控制管理,都是很简单的东西,一步一步写就不会错了,完成速度相当于打字速度。
looking out of the window at the bleak scenery. Suddenly, a note caught his attention from the lower right corner of the screen. It said: “You have 1 unread email message(s)”. The boy braced himself for a bit of useless spam and opened the letter. But it turned
out much more interesting...
Dear Sir, You have received this message from the “Rutnok BKS” HR department!
We have received your application for a software developer and we found your CV quite interesting. We would like to suggest a simple test task to evaluate your professional skills. Your task is to implement
the register system for a forum. It must support three types of operations:
“register username password”: to register a new user with name “username” and password “password”. If such user already exists in the database, the system should output the error message “fail: user already exists”. Otherwise, it should output message “success:
new user added”.
“login username password”: to log into the system as user “username” with password “password”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user enters an incorrect password, the system should output “fail:
incorrect password”. Otherwise, if the user is already logged in the system at that moment, it should output “fail: already logged in”. Otherwise, it should output “success: user logged in”.
“logout username”: to log out of the system as user “username”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user isn’t in the system at that moment, it should output “fail: already logged out”. Otherwise,
it should output “success: user logged out”.
Use this letter as a formal description of the algorithm and follow the described format of system messages. Good luck!
Ilya stopped doing anything else and started solving the test task. You can try to solve it as well!
Input
The first line contains an integer n that is the number of operations (1 ≤ n ≤ 100). Each of the next n lines contains a single query in the format given above. We can assumethat “username” and “password” are non-empty strings with length of up to 30 characters. All characters in these strings have codes from 33 to 126.
Output
For each operation, print the message on a single line in the format given above. Be sure to put spaces and punctuation marks in the right places in the messages.
Sample
input | output |
---|---|
6 register vasya 12345 login vasya 1234 login vasya 12345 login anakin C-3PO logout vasya logout vasya | success: new user added fail: incorrect password success: user logged in fail: no such user success: user logged out fail: already logged out |
思路:
1 使用map,在map中的就是已经注册的了
2 使用数据结构保存用户名,是否登陆和密码
3 使用if else判断处理第一个字符串-命令
类似很多人都写的什么图书馆管理系统,什么信息系统之类的登陆控制管理,都是很简单的东西,一步一步写就不会错了,完成速度相当于打字速度。
#include <string> #include <vector> #include <cmath> #include <algorithm> #include <iostream> #include <unordered_map> #include <unordered_set> using namespace std; namespace { struct States { bool logined; string name; string pw; States(string n="", string p="", bool l = false): name(n), pw(p), logined(l){} }; } void TestTask2002() { int opTimes = 0; cin>>opTimes; unordered_map<string, States> umSS; string command, name, pw; while (opTimes--) { cin>>command>>name; if ("register" == command) { cin>>pw;//logout不用pw的 if (umSS.count(name)) cout<<"fail: user already exists\n"; else { States st(name, pw); umSS[name] = st; cout<<"success: new user added\n"; } } else if ("login" == command) { cin>>pw; if (!umSS.count(name)) cout<<"fail: no such user\n"; else if (pw != umSS[name].pw) cout<<"fail: incorrect password\n"; else if (umSS[name].logined) cout<<"fail: already logged in\n"; else { cout<<"success: user logged in\n"; umSS[name].logined = true; } } else if ("logout" == command) { if (!umSS.count(name)) cout<<"fail: no such user\n"; else if (!(umSS[name].logined)) cout<<"fail: already logged out\n"; else { cout<<"success: user logged out\n"; umSS[name].logined = false; } } } }
相关文章推荐
- sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛
- phoneGap 安装
- Android 网络编程
- WinXP系统开机自动注销使用安全模式也不行怎么办
- websphere出现sun.io.MalformedInputException
- 一些常用到的库记录
- TDD:simply mocking a class is not necessarily the best practice
- 用Delphi将数据导入到Excel并控制Excel
- 连接数据库修改篇
- Spring源码学习
- 新版谷歌地图的好处和优点
- Qt: 释放窗口资源
- Android推送解决方案分析-一共介绍6个
- linux设备模型bus,device,driver
- SpringMvc showcase -- Mapping Controller
- C/C++ linux下光标定位和清屏函数(printf 实现)
- java 快速排序算法
- SSH环境搭建,配置整合初步(一)
- MVC中DbContext 和ObjectContext两者的区别!
- Android 实例解说Application类