当前位置:网站首页>PAT B1067
PAT B1067
2022-06-25 19:57:00 【Madness makes freedom】
1067 Try password (20 branch )
When you try to log in to a system and forget your password , The system will generally only allow you to try a limited number of times , When the allowed number of times is exceeded , The account will be locked . This topic asks you to realize this small function .
Input format :
Enter a password on the first line ( Length not exceeding 20 Of 、 No spaces 、Tab、 Non empty string of carriage return ) And a positive integer N(≤ 10), They are the correct password and the number of attempts allowed by the system . Then each line gives a non empty string ending with carriage return , Is the password the user tried to enter . Input guarantees at least one attempt . When you read a single line # Character time , End of input , And this line is not user input .
Output format :
Every input to the user , If the password is correct and the number of attempts does not exceed N, Output in one line Welcome in
, And end the program ; If it's wrong , Then output in one line in format Wrong password: Wrong password entered by the user
; When the wrong attempt reaches N When the time , One more line Account locked
, And end the program .
sample input 1:
Correct%pw 3
correct%pw
[email protected]
whatisthepassword!
Correct%pw
#
sample output 1:
Wrong password: correct%pw
Wrong password: [email protected]
Wrong password: whatisthepassword!
Account locked
sample input 2:
[email protected] 3
[email protected]
[email protected]
[email protected]
try again
#
sample output 2:
Wrong password: [email protected]
Wrong password: [email protected]
Welcome in
There are several points to note in this question !!!!
- Enter the first string , Use cin Input , Because the string input is followed by the input times .
- For the answer entered by the user later , It is necessary to use getline Function input , Because the title only says the non empty string that has ended with carriage return , So there may be a space in the middle , By experiment , It is true that the data is composed of spaces . I used it at first cin Input .
- For the last output , There is one caveat , When the number of input errors reaches the required number ; Then you will output
Account locked
Can't wait for n+1 And then enter again , Otherwise, a set of data will fail , Here is the code to understand . That is the third code . At first I never understood , I think it's OK to write like this . Until I read the title , Just gradually understand .
I have written two codes in total , In fact, it's the same . Why write two , The first is input and then output , The second is to input all the data before processing . When I run the first code , My compiler can't pass , Although in PAT There is only one wrong group on the , The one mentioned above . But I have OCD , You have to see the result to calm down .
This is what I changed later , Code that can pass all !!!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string correct,user_cin;
int n,count=0;
cin >> correct >> n;
getchar();
while(1)
{
++count;
//cin >> user_cin;
getline(cin,user_cin);
if(user_cin=="#")
{
break;
}
if(user_cin==correct)
{
cout << "Welcome in\n";
break;
}
else
{
cout << "Wrong password: " << user_cin << endl ;
if(count==n)
{
cout << "Account locked\n";
break;
}
}
}
return 0;
}
This is the code for unified processing after all inputs !
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string correct,in;
string user_cin[100];
int n,count=0,i=0;
cin >> correct >> n;
getchar();
while(getline(cin,in))
user_cin[i++]+=in;
for(int j=0;j<i;++j)
{
++count;
if(user_cin[j]=="#")
{
break;
}
if(count<=n)
{
if(user_cin[j]==correct)
{
cout << "Welcome in\n";
break;
}
else
{
cout << "Wrong password: " << user_cin[j] << endl ;
if(count==n)
{
cout << "Account locked\n";
break;
}
}
}
}
return 0;
}
This is the first time that a set of data failed to pass the code !!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string correct,user_cin;
int n,count=0;
cin >> correct >> n;
getchar();
while(1)
{
++count;
//cin >> user_cin;
getline(cin,user_cin);
if(user_cin=="#")
{
break;
}
if(count<=n)
{
if(user_cin==correct)
{
cout << "Welcome in\n";
break;
}
else
cout << "Wrong password: " << user_cin << endl ;
}
else
{
cout << "Account locked\n";
break;
}
}
return 0;
}
边栏推荐
- Applet request interface encapsulation
- Jsonp processing non homologous
- Native JS array some method de duplication
- 2.15(Multiple of 3 Or 5)
- Applet canvas generate sharing Poster
- PostgreSQL user role permissions
- Database data type design (the most detailed in the whole network)
- ActiveMQ--CVE-2016-3088
- Alicloud centos8.0 installing mysql8
- Browser performance optimization (19)
猜你喜欢
On Oracle full stack virtual machine -- graalvm
Process of vacuum and vacuum full
The native JS mobile phone sends SMS cases. After clicking the button, the mobile phone number verification code is sent. The button needs to be disabled and re enabled after 60 seconds
Print 1 cute every 100 milliseconds ~ with a running lantern effect
Laravel validation rule followed Role of auth:: id()
Use of serialize() and serializearray() methods for form data serialization
Jsonp processing non homologous
Using flex to implement the Holy Grail layout is as simple as that
Google cloud SSH enable root password login
2.14(Knight Moves)
随机推荐
通过启牛学堂开的股票账户可以用吗?资金安全吗?
Embark on a new journey and reach the world with wisdom
The meanings of /32, /48, /64 in IPv6 addresses
How to understand var = a = b = C = 9? How to pre parse?
Vulnhub range the planes: mercury
Jump jump games auxiliary (manual version) py code implementation
Server journey from scratch - Yu Zhongxian integrated version (IP access server, LNMP compilation and installation, Lua environment and socket expansion)
Divine reversion EA
DataX script task development record
2、 Hikaricp source code analysis of connection acquisition process II
Please do not call Page constructor in files
Determine whether it is a web page opened on wechat
Yum command
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
Uniapp waterfall flow, applet waterfall flow, very simple, suitable for the whole platform
Wechat applet connects to the server to display mqtt data information
Dependency injection in PHP reflection implementation framework
System optimization method
Does GoogleSEO need to change the friend chain? (e6zzseo)
Vulnhub range the planes:earth