当前位置:网站首页>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;
}

原网站

版权声明
本文为[Madness makes freedom]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190512260372.html